当我将 use Blog;
放在顶部时出现此错误。
Warning: The use statement with non-compound name 'Blog' has no effect in...
Blog
是我的命名空间,其中有 3 个类:文章、列表和类别以及一些函数。
如果我将 statememnt 更改为 use Blog\Article;
那么它可以工作...
我不能只指定我想使用的命名空间吗?我需要提供类(class)吗?
如果我在该命名空间中有函数怎么办?当我在命名空间之外调用它们时,我不得不在每个人的名字前面加上 \Blog\
...
最佳答案
PHP 的 use
与 C++ 的 using namespace
不同;它允许您定义一个别名,而不是“导入”一个命名空间,因此从此以后完全省略命名空间限定符。
所以,你可以这样做:
use Blog\Article as BA;
...缩短它,但你不能完全摆脱它。
因此,使用博客
是没用的,但我相信你可以写:
use \ReallyLongNSName as RLNN;
请注意,您必须在此处使用前导 \
来强制解析器知道 ReallyLongNSName
是完全限定的。 Blog\Article
则不是这样,它显然已经是一个命名空间链:
Note that for namespaced names (fully qualified namespace names containing namespace separator, such as
Foo\Bar
as opposed to global names that do not, such asFooBar
), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.
关于php - 故障排除 "The use statement with non-compound name ... has no effect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317022/