Coder Perfect

“The use statement with a non-compound name… has no impact” Troubleshooting


When I put use Blog; at the top, I get this problem.

My namespace, Blog, contains three classes: Article, List, and Category, as well as a few functions.

It works if I update my statement to utilize BlogArticle…

Isn’t it possible to just declare the namespaces I’d like to use? Is it necessary for me to conduct classes?

What if those namespaces contain functions? I’m obliged to add the word “Blog” to each of their names when I call them outside of the namespace…

Asked by thelolcat

Solution #1

PHP differs from C++ in that it allows you to define an alias rather than “import” a namespace, allowing you to avoid the namespace qualifier entirely.

So, here’s what you could do:

use Blog\Article as BA;

… to abbreviate it, but you won’t be able to completely eliminate it.

As a result, using Blog is pointless, but I think you could write:

use \ReallyLongNSName as RLNN;

It’s worth noting that you’ll need to use a leading slash here to convince the parser that ReallyLongNSName is fully qualified. This isn’t the case with BlogArticle, which is already a namespace chain:

Answered by Lightness Races in Orbit

Solution #2

Because this query is the first result for this mistake on Google, I’ll explain how I fixed it.

Basically, if you’re using a framework like Yii2, you’ll be used to declaring classes like:

use Yii;
use yii\db\WhatEver;

class AwesomeNewClass extends WhatEver

Because this class has no namespace, you’ll get this error while using Yii.

Because this class has no namespace, it inherits the global symbol table and hence does not require anything like this to be defined; simply remove it.

Answered by Sammaye

Solution #3

In PHP, the use statement is simply a way to alias a large namespace into something that is a little easier to read. Apart from providing convenience, it doesn’t actually include any files or do anything else that has an impact on your progress. Because Blog isn’t aliased as anything, you don’t get any of the benefits. I’m sure you could do something similar.

utilize the letter B in your blog;

And it’s possible that this will work. (It may be argued that by obscuring, you lose convenience, but that isn’t the point of the discussion.) Because you’re actually aliasing the Blog namespace to something else. Using BlogArticle is effective because, according to the documentation,

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

As a result, your snippet would be:

use Blog\Article as Article;

Answered by Charles Sprayberry

Solution #4

If you don’t want to use the ‘as’ syntax, you can use

use \Blog as B;

define the file’s namespace

namespace anyname;

use Blog

Answered by samehanwar

Solution #5

If you try to use a trait before a class definition, the error “The use statement… has no impact…” appears.

use My_trait; // should not be here

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

Answered by Dieter Donnert

Post is based on