Chris Schmitz Chris Schmitz - 5 months ago 51
PHP Question

Laravel/Composer: The use statement with non-compound name

In my laravel project I created a model called

CustomerLinks
. The model resides in the
app/models
folder. My composer file has an autoload of:

"autoload": {
"classmap": [
...
"app/models",
...

],
...
},


And I have a
use
statement in my ExtendedUserController that references
CustomerLinks
:

<?php

...
use CustomerLinks;
...

class ExtendedUserController extends UserController {


It's my understanding that since the autoload property in the composer file has app/models in the classmap it means I should be able to use
use CustomerLinks
without a namespace prefix.

This works, but any time I make a change to my ExtendedUserControler and reload my browser I get the error:

The use statement with non-compound name 'CustomerLinks' has no effect


The error points to the
use CustomerLinks
line extended user controller.

When I do a
composer dump-autoload
everything works fine, but it becomes extremely irritating when I have to follow the pattern


make a change -> dump autoload -> refresh browser -> repeat


Is there some way of dealing with the error?

Answer

If you are not within a namespace (i.e. you are in the root namespace), and the class you want to use also is not in a namespace (i.e. also in the root namespace), then using use makes no sense, because the code will work the same without it. You are not importing anything with this statement.

Composer has nothing to do with this, neither has any other autoloading. It's how PHP works by itself.

Comments