Home >Backend Development >PHP Tutorial >Interesting facts about namespaces

Interesting facts about namespaces

WBOY
WBOYOriginal
2016-08-04 09:21:561257browse

I found an interesting question. Within a file without a namespace.

<code>use PDO; 或者 use \PDO;
$conn = new PDO(....);
会报出一个错误 Warning: The use statement with non-compound name 'PDO' has no effect......

然而下面几种做法不会报这个错误
use PDO as PDO;
use \PDO as PDO;
或者
直接 $conn = new PDO(....);
$conn = new \PDO(....);
都是不会报错的。
</code>

Then the question comes, why can’t you use PDO for files without namespace;

Updated on July 11, 2016 22:18:51 ----------
The above test is in the php7 environment.
It is possible to use PDO in 5.4 environment, but use PDO still doesn’t work

Reply content:

I found an interesting question. Within a file without a namespace.

<code>use PDO; 或者 use \PDO;
$conn = new PDO(....);
会报出一个错误 Warning: The use statement with non-compound name 'PDO' has no effect......

然而下面几种做法不会报这个错误
use PDO as PDO;
use \PDO as PDO;
或者
直接 $conn = new PDO(....);
$conn = new \PDO(....);
都是不会报错的。
</code>

Then the question comes, why can’t you use PDO for files without namespace;

Updated on July 11, 2016 22:18:51 ----------
The above test is in the php7 environment.
It is possible to use PDO in 5.4 environment, but use PDO still doesn’t work

http://stackoverflow.com/questions/9317022/troubleshooting-the-use-statement-with-non-compound-name-has-no-effect

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn