doushi9856 2014-02-27 21:27
浏览 87
已采纳

php命名空间无法找到SQLite3类

I have searched and read through many posts here regarding PHP namespaces and accessing class members. I still can't reference the SQLite3 class when wrapped in a custom class and contained within a namepace. Example below:

sqlite3_data_broker.php

namespace DataAccess\SQLiteDb;

class SQLiteBroker {

    private $db, $db_pathname;

    public function __construct ($dbPathName) {
        $this->db_pathname = $dbPathName;
    }

    public function Open() {
        $this->db = new SQLite3($this->db_pathname);
    }


    public function getPathName() {
        return $this->db_pathname;
    }

}

some_other_class.php

include('sqlite3_data_broker.php');

$the_db = new \DataAccess\SqliteDb\SQLiteBroker('K:\Path\To\Db\the_data.db');

echo 'Database Full Name: ' . $the_db->getPathName();

$the_db->Open();

Will yield the following:

Database Full Name: K:\Path\To\Db\the_data.db

Fatal error: Class 'DataAccess\SQLite3Db\SQLite3' not found in sqlite3_data_broker.php on line...

Using a namespace I can reference class members but not SQLite3 class wrapped in the open() function. If I remove the namespace and references to it, that is call the broker class like this: $the_db = new SQLiteBroker('K:\Path\To\Db\the_data.db'), all works fine.

Is there something I'm missing or not considering in wrapping the SQLite3 class? If it comes down to it I'll remove the namespace, though it would be nice to have it.

PHP Version 5.4.6.

  • 写回答

1条回答 默认 最新

  • dthok9648 2014-02-27 21:32
    关注

    Fully qualified name with global prefix operator \:

    $this->db = new \SQLite3($this->db_pathname);
    

    http://php.net/manual/en/language.namespaces.basics.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog