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

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料