我的自动加载规范如下
"autoload" : { "psr-4" : { "MyMVC\\" : "app/" }, "classmap": [ "app/Controllers", "app/Helpers" ], "files": ["app/routes.php"] },routes.php文件的内容是:
<?php use MyMVC\Core\Route; $route = new Route; $route->add('/', 'HomeController@index'); $route->add('about', 'AboutController@index'); $route->add('contact', 'ContactController@index');现在在我的app/init.php我试图使用$route对象,但它给了我错误
Notice: Undefined variable: route in /var/www/html/mymvc/app/init.php on line 29以下是我尝试使用$route对象的方法。
/** * Constructor * Bootstrap our application based on the configurations provided */ public function __construct() { // require 'app/routes.php` This will work fine but it should be autoloaded var_dump($route); exit; }我还运行了命令composer dump-autoload
My Autoload specification are as follows
"autoload" : { "psr-4" : { "MyMVC\\" : "app/" }, "classmap": [ "app/Controllers", "app/Helpers" ], "files": ["app/routes.php"] },The contents of routes.php file are:
<?php use MyMVC\Core\Route; $route = new Route; $route->add('/', 'HomeController@index'); $route->add('about', 'AboutController@index'); $route->add('contact', 'ContactController@index');now in my app/init.php i am trying to use the $route object but its giving me error
Notice: Undefined variable: route in /var/www/html/mymvc/app/init.php on line 29Here is how i am trying to use the $route object.
/** * Constructor * Bootstrap our application based on the configurations provided */ public function __construct() { // require 'app/routes.php` This will work fine but it should be autoloaded var_dump($route); exit; }I have also ran command composer dump-autoload
最满意答案
自动加载在这里不起作用。 PHP只能自动加载类。 您不希望app/routes.php被自动加载,因为该文件不包含类声明,并且您无法使用以前未知的类触发它的执行。
确实,当您包含vendor/autoload.php时,Composer将执行该文件一次 - 但是,这是您软件的非常糟糕的行为。 不要使用“文件”自动加载来包含配置文件。 注意在图书馆中使用时可能产生的性能影响。 您应该完全避免使用它,它应该用于遗留代码,否则无法使其工作。
另一方面,您的架构已经崩溃。 你不应该通过访问应该在其他地方初始化的变量来编写一个“神奇地”了解配置的类。 一个好的模式是将配置作为参数传递给构造函数:
public function __construct ($routes) { $this->routes = $routes; }创建此类的代码部分应该从某处获取配置并将其作为参数传递。 这个概念被称为控制或依赖注入的反转:类不会调用他们需要使用的其他类,他们要求它们并将它们作为参数。
Autoloading won't work here. PHP can only autoload classes. Your expectation that app/routes.php will be autoloaded is not possible, because that file does not contain a class declaration, and you are not able to trigger it's execution by using a previously unknown class.
It is true that Composer will execute that file once when you include vendor/autoload.php - however, this is really bad behavior of your software. Don't use the "files" autoloading to include configuration files. Mind the performance impact this may have when being used in libraries. You should avoid using it altogether, it is meant to be used for legacy code that cannot otherwise be made working.
On the other hand, your architecture is broken. You shouldn't write a class that "magically" knows about the configuration just by accessing a variable that is supposed to be initialized somewhere else. A good pattern would be to pass the configuration as a parameter to the constructor:
public function __construct ($routes) { $this->routes = $routes; }The part of the code that creates this class is supposed to grab the configuration from somewhere and pass it as a parameter. This concept is called inversion of control or dependency injection: Classes do not invoke the other classes they need to work with, they ask for them and get them as a parameter.
更多推荐
发布评论