php - 故障排除 "The use statement with non-compound name ... has no effect"

标签 php namespaces

当我将 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 as FooBar), 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/

相关文章:

PHP 和 MySQL - fatal error : Function name must be a string

php - PRESTASHOP:如何告诉 PDO 类通过套接字而不是 'localhost' 进行连接?

javascript - 防止对 php 的垃圾 XMLHTTP 请求

php - Laravel/Heroku,没有这样的文件或目录用于 session

c# - XSD 命名空间到 C# 命名空间

php - 如何从 PHP 中的关联数组中删除具有空键的值?

带有命名空间的 PHP 自动加载不加载文件

c++ - 为什么要在命名空间中使用类

laravel - 可以访问 Laravel 4 包中的文件

php - Laravel 5 应用命名空间