Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

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

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

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

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

If I change my statememnt to use BlogArticle; then it works...

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

PHP's use isn'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.

So, you could do:

use BlogArticle as BA;

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


Consequently, use Blog is useless, but I believe you could write:

use ReallyLongNSName as RLNN;

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

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as FooBar 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...