Home >Backend Development >PHP Tutorial >Interesting facts about namespaces
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
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