简体   繁体   English

使用PSR-4找不到类

[英]Class Not Found using PSR-4

I have a PSR-4 specification in my composer.json file as below 我的composer.json文件中有一个PSR-4规范,如下所示

"autoload" : {
    "psr-4" : {
        "MyMVC\\" : "app/"
    }
},

在此输入图像描述

Above is my directory structure. 以上是我的目录结构。 In my Core/Config.php file i have class Config that is under namespace MyMVC\\Core . 在我的Core / Config.php文件中,我有一个位于命名空间MyMVC\\Core下的类Config。 (Just taking Config class as example in question, this is same for all classes). (仅以Config类为例,所有类都是一样的)。

Now in my Config/config.php file i am using below code 现在在我的Config / config.php文件中,我正在使用下面的代码

<?php
use MyMVC\Core;

Config::$config['base_url'] = 'http://localhost/mymvc';

But this gives me error of Class Config Not Found. 但这给了我错误的Class Config Not Found。 The problem can be fixed if i use MyMVC\\Core\\Config; 如果我使用MyMVC\\Core\\Config;问题可以解决MyMVC\\Core\\Config; . But it should work without using Config explicitly. 但它应该在不明确使用Config的情况下工作。 Since there can be files added by the framework user which are supposed to be autoloaded. 由于框架用户可以添加应该自动加载的文件。

Thanks 谢谢

The use primitive imports or aliases a namespace or class. use原语导入或别名命名空间或类。 As the manual states: 手册所述:

PHP supports three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. PHP支持三种别名或导入:别名类名,别名接口名和别名命名空间名。 PHP 5.6+ also allows aliasing or importing function and constant names. PHP 5.6+还允许别名或导入函数和常量名称。

Your use statement is "aliasing a namespace". 您的use语句是“别名命名空间”。 So 所以

use MyMVC\Core;

Is the same as: 是相同的:

use MyMVC\Core as Core;

Thus in your code: 因此在你的代码中:

Config::$config['base_url'] = 'http://localhost/mymvc';

Should be: 应该:

Core\Config::$config['base_url'] = 'http://localhost/mymvc';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM