php Laravel/Composer:具有非复合名称的 use 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23745050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel/Composer: The use statement with non-compound name
提问by Chris Schmitz
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:
在我的 Laravel 项目中,我创建了一个名为CustomerLinks
. 模型驻留在app/models
文件夹中。我的作曲家文件有一个自动加载:
"autoload": {
"classmap": [
...
"app/models",
...
],
...
},
And I have a use
statement in my ExtendedUserController that references CustomerLinks
:
我use
在我的 ExtendedUserController 中引用了一条语句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.
我的理解是,由于 Composer 文件中的 autoload 属性在类映射中有 app/models,这意味着我应该能够在use CustomerLinks
没有命名空间前缀的情况下使用。
This works, but any time I make a change to my ExtendedUserControler and reload my browser I get the error:
这有效,但任何时候我对 ExtendedUserControler 进行更改并重新加载浏览器时,都会收到错误消息:
The use statement with non-compound name 'CustomerLinks' has no effect
The error points to the use CustomerLinks
line extended user controller.
错误指向use CustomerLinks
线路扩展用户控制器。
When I do a composer dump-autoload
everything works fine, but it becomes extremely irritating when I have to follow the pattern
当我做一个composer dump-autoload
一切正常时,但当我必须遵循模式时它变得非常烦人
make a change -> dump autoload -> refresh browser -> repeat
进行更改 -> 转储自动加载 -> 刷新浏览器 -> 重复
Is there some way of dealing with the error?
有没有办法处理错误?
回答by Sven
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.
如果你不在命名空间中(即你在根命名空间中),并且你想要使用的类也不在命名空间中(即也在根命名空间中),那么 usinguse
没有意义,因为代码会起作用没有它也一样。您没有使用此语句导入任何内容。
Composer has nothing to do with this, neither has any other autoloading. It's how PHP works by itself.
Composer 与此无关,也没有任何其他自动加载。这就是 PHP 本身的工作方式。