php 疑难解答“具有非化合物名称的 use 语句......无效”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9317022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:32:48  来源:igfitidea点击:

Troubleshooting "The use statement with non-compound name ... has no effect"

phpnamespaces

提问by thelolcat

Getting this error when I put use Blog;at the top.

当我放在use Blog;顶部时出现此错误。

Warning: The use statement with non-compound name 'Blog' has no effect in...

警告:具有非复合名称“博客”的 use 语句在...

Blogis my namespace in which I have 3 classes: Article, List and Category and a few functions.

Blog是我的命名空间,其中有 3 个类:文章、列表和类别以及一些函数。

If I change my statememnt to use Blog\Article;then it works...

如果我将我的声明更改为use Blog\Article;那么它会起作用...

Can't I just specify the namespaces I want to use? Do I need to provide classes?

我不能只指定我想要使用的命名空间吗?我需要提供课程吗?

What if I have functions within that namespaces? When I call them outside of the namespace, I'm forced to prepend \Blog\to each one's name...

如果我在该命名空间中有函数怎么办?当我在命名空间之外调用它们时,我被迫\Blog\在每个人的名字前面加上......

采纳答案by Lightness Races in Orbit

PHP's useisn't the same as C++'s using namespace; it allows you to define an alias, not to "import" a namespace and thus henceforth omit the namespace qualifier altogether.

PHPuse与 C++ 不同using namespace;它允许您定义别名,而不是“导入”名称空间,因此从此完全省略名称空间限定符。

So, you could do:

所以,你可以这样做:

use Blog\Article as BA;

... to shorten it, but you cannot get rid of it entirely.

...缩短它,但你不能完全摆脱它。



Consequently, use Blogis useless, but I believe you could write:

因此,use Blog没用,但我相信你可以写:

use \ReallyLongNSName as RLNN;

Note that you must use a leading \here to force the parser into knowing that ReallyLongNSNameis fully-qualified. This isn't true for Blog\Article, which is obviously already a chain of namespaces:

请注意,您必须\在此处使用前导来强制解析器知道它ReallyLongNSName是完全限定的。对于 来说Blog\Article,情况并非如此,这显然已经是一个命名空间链:

Note that for namespaced names(fully qualified namespace names containing namespace separator, such as Foo\Baras opposed to global names that do not, such as FooBar), the leading backslash is unnecessaryand not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

请注意,对于命名空间名称(包含命名空间分隔符的完全限定命名空间名称,例如与不包含命名空间分隔符的Foo\Bar全局名称相反,例如FooBar),前导反斜杠是不必要的,不推荐使用,因为导入名称必须是完全限定的,并且不会被处理相对于当前命名空间。

回答by Sammaye

Since this question appears as the first result on Google for this error I will state how I fixed it.

由于此问题是 Google 上针对此错误的第一个结果,因此我将说明我是如何修复它的。

Basically if you have a framework, say like Yii2 you will be used to having to do declare classes like:

基本上,如果你有一个框架,比如 Yii2,你将习惯于像这样声明类:

use Yii;
use yii\db\WhatEver;

class AwesomeNewClass extends WhatEver
{
}

You will get this error on Use Yiisince this class has no namespace.

Use Yii由于此类没有命名空间,因此您将收到此错误。

Since this class has no namespace it automatically inherits the global symbol table and so does not need things like this defining, just remove it.

由于这个类没有命名空间,它会自动继承全局符号表,因此不需要像这样定义的东西,只需将其删除即可。

回答by Charles Sprayberry

The usestatement in PHPis really just a convenience to alias a long namespace into something that may be a little easier to read. It doesn't actually include any files or do anything else, that effects your development, besides providing convenience. Since, Blogisn't aliased as anything you aren't gaining any of the convenience. I could imagine you could do something like

usePHP 中语句实际上只是为了方便地将长命名空间别名为可能更易于阅读的内容。除了提供便利之外,它实际上不包含任何文件或执行任何其他影响您的开发的事情。因为,Blog没有别名为您没有获得任何便利的任何东西。我可以想象你可以做类似的事情

use \Blog as B;

use \Blog as B;

And that may even work. (It could be argued you actually lose convenience here by obscuring but that's not what the question is about) Because you're actually aliasing the Blognamespace to something else. Using Blog\Articleworks because, according to the docs:

这甚至可能奏效。(可能有人会争辩说,您实际上通过遮蔽而失去了便利性,但这不是问题所在)因为您实际上是将Blog名称空间别名为其他名称。使用Blog\Article作品是因为,根据文档:

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

So your snippet would be equivalent to:

所以你的代码段相当于:

use Blog\Article as Article;

回答by Dieter Donnert

The error "The use statement ... has no effect..." also pops up if you try to use a trait before a class definition.

如果您尝试在类定义之前使用特征,也会弹出错误“使用语句...没有效果...”。

use My_trait; // should not be here

class My_class{
// use My_trait; should be here instead
}

回答by samehanwar

if you don't want to use 'as' syntax like

如果您不想使用“as”语法,例如

use \Blog as B;

define a namespace for the file

为文件定义命名空间

namespace anyname;

use Blog