PHP: “use” in include files? [duplicate]

亡梦爱人 提交于 2019-12-24 03:26:12

问题


When using namespaces in PHP you can do things like:

<?php
use \mynamspace\MyClass;
$a = new MyClass();
?>

Is it possible to put the use... line in an include file? If there are dozens oft those use statements it is very annoying to write them in every single file you need them.

But the use statement is only working for the current file and is ignored in the files that include it.

Any ideas?


回答1:


Unfortunately not. PHP's namespace constructs are in a per file basis. The PHP manual sums it up as such:

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

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



来源:https://stackoverflow.com/questions/21044229/php-use-in-include-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!