Troubleshooting "The use statement with non-compound name ... has no effect" [duplicate]
Asked Answered
M

6

125

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

Melanous answered 16/2, 2012 at 18:34 Comment(3)
Happened to me when I declared my file namespace after use Blah;.Pyxidium
It can also just mean that 'Blog' has already been included already and you're trying to use it a second time. This is common if you're using a framework that already included it higher up the chain.Nix
@DerekIllchuk if only this could be an answer which I could upvote. Saved me a lot of headache.Spent
A
110

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.

Alkalinity answered 16/2, 2012 at 18:42 Comment(3)
Ah that sucks. So the use thing is actually useless, might as well be an acronym for that :)Melanous
@thelolcat: It has its uses... just not the one that you want. :) It does seem like it could be an annoying limitation.Alkalinity
This answer is only correct for non-namespaced files. 1) In a namespaced file, there is no need to use a leading \ in the use statement, because its arguments are always seen as absolute (i.e., starting from the global namespace). 2) use Blog; is not necessarily useless: for example, from a file namespaced as Blog\Util\CLI, it would enable you to write Blog\Entry::method() instead of \Blog\Entry::method(). Not that this is really necessary, but it does have an effect. For an example of this usage, see the Nette framework.Contradiction
C
38

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.

Colemancolemanite answered 16/2, 2014 at 11:19 Comment(7)
For me, the problem was that I was trying to use a symbol from the global namespace, e.g. use \Password but php doesn't like it when you try to do that without an alias, so even use \Password as Password works just fine.Hyperbolic
Downvoted for suggesting use of OOD when moderator was looking for a more general answer with regard to use of namespaces.Civility
@JoshH I did explain that I posted this because this question is the first google resultColemancolemanite
Understood. And that makes sense. harmful.cat-v.org/software/OO_programmingCivility
@JoshH what are you on about? I don't even get your last reply. You have gone from saying it is not what some moderator was looking for to giving some guy's link about OOP trollingColemancolemanite
@JoshH (BTW I hate it when people reference opinionated blogs like that as authoritative material for answering a question)Colemancolemanite
@Colemancolemanite Sorry if I offended you in any way. I just started writing PHP and noticed it has had closures for about 9 years yet every resource I come across reeks of OOD. OP asked "Do I need to provide classes?", and the answer to that question in general is no.Civility
D
9

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;
Decasyllabic answered 16/2, 2012 at 18:43 Comment(0)
I
6

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

use \Blog as B;

define a namespace for the file

namespace anyname;

use Blog
Imp answered 2/4, 2017 at 14:47 Comment(0)
G
2

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
}
Glabrate answered 8/7, 2016 at 2:1 Comment(0)
J
2

Perhaps it's something like

namespace Path\To\Your\Namespace\Blog;

use Blog; // Redundant

class Post {
    public $linkedArticle;

    public function __construct($article = null)
    {
        $this->linkedArticle = $article ?? new Blog\Article();
    }
}

Blog is already available, because that's the namespace you're in, so you can use new Blog\Article(); without use Blog; at the top. That's exactly what the error tells you - the added line has no effect.

Pointless:

use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace;

Useful:

use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace as Phew;

If you on the other hand wish to use new Article() then you can do it like this.

namespace Path\To\Your\Namespace\Blog;

use Blog\Article; // Equivalent to "use Blog\Article as Article;"

class Post {
    public $linkedArticle;

    public function __construct($article = null)
    {
        $this->linkedArticle = $article ?? new Article();
    }
}

In practice you'd do something like

// Fairly separated domains
use Some\TooLong\Namespace\App\User;
use Some\TooLong\Namespace\App\Ecommerce;
use Some\TooLong\Namespace\App\Auth;

but not necessarilly

// Two tools in same domain
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 ;)

Judon answered 9/9, 2021 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.