php Namespace Cannot Find SQLite3 Class

PHP命名空间不能找到sqlite3类

问题 (Question)

Hello Stackoverflow forum,

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 contined 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 memebers, 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, it would be nice to have it though.

PHP Version 5.4.6.

Thanks and regards.

你好,StackOverflow论坛,

我在读了许多关于PHP的命名空间的帖子,和访问类的成员。我仍然不能引用sqlite3上课时,包裹在一个自定义的类,并装在namepace。下面的例子;

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();

将产生如下;

数据库的全名:K:\路径\ \ \ the_data.db分贝

致命错误:类的数据访问、sqlite3db \ sqlite3 '中找不到sqlite3_data_broker.php线…

使用命名空间,我可以引用类的成员,但不是sqlite3类包裹在open()功能。如果我删除命名空间,并对它的引用,这是给经纪人打电话这样的类,* $ the_db =新sqlitebroker(k:\ \ \ the_data分贝。分贝路径)*,所有工程罚款。

有我丢失的东西或不考虑在包装程序类?

如果它真的来了,我会删除命名空间,它会把它虽然是好的。

PHP版本5.4.6。

感谢和问候。

最佳答案 (Best Answer)

Fully qualified name with global prefix operator \:

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

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

完全合格的名称与全局前缀操作符\

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

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

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22080817