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

PhpNamespaces

Php Problem Overview


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 Blog\Article; 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...

Php Solutions


Solution 1 - Php

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 Blog\Article 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 Blog\Article, which is obviously already a chain of namespaces:

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

Solution 2 - Php

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

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

use Yii;
use yii\db\WhatEver;

class AwesomeNewClass extends WhatEver
{
}

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

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.

Solution 3 - Php

The use statement in PHP is 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, Blog isn't aliased as anything you aren't gaining any of the convenience. I could imagine you could do something like

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 Blog namespace to something else. Using Blog\Article works because, according to the docs:

// 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;

Solution 4 - Php

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

use \Blog as B;

define a namespace for the file

namespace anyname;

use Blog

Solution 5 - Php

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
}

Solution 6 - Php

Blog is already available, so you can use $article = new Blog\Article(); without use Blog; at the top. That's exactly what the error tells you - the added line has no effect. It's pointless to use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace; unless you wish to alias it with use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace as Phew;.

If you on the other hand wish to use $article = new Article(); then use stands for "useful" (joke) and you must do use Blog\Article;, which is equivalent to use Blog\Article as Article;

In practice you'd do something like

use Some\TooLong\Namespace\App\User;
use Some\TooLong\Namespace\App\Ecommerce;
use Some\TooLong\Namespace\App\Auth;

but not necessarilly

use Some\TooLong\Namespace\App\Ecommerce\Cart;
use Some\TooLong\Namespace\App\Ecommerce\Checkout;

as well. I'm positive there are better examples than this ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthelolcatView Question on Stackoverflow
Solution 1 - PhpLightness Races in OrbitView Answer on Stackoverflow
Solution 2 - PhpSammayeView Answer on Stackoverflow
Solution 3 - PhpCharles SprayberryView Answer on Stackoverflow
Solution 4 - PhpsamehanwarView Answer on Stackoverflow
Solution 5 - PhpDieter DonnertView Answer on Stackoverflow
Solution 6 - Phps3cView Answer on Stackoverflow