简体   繁体   中英

Class Not Found using PSR-4

I have a PSR-4 specification in my composer.json file as below

"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 . (Just taking Config class as example in question, this is same for all classes).

Now in my Config/config.php file i am using below code

<?php
use MyMVC\Core;

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

But this gives me error of Class Config Not Found. The problem can be fixed if i use MyMVC\\Core\\Config; . But it should work without using Config explicitly. 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. 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 5.6+ also allows aliasing or importing function and constant names.

Your use statement is "aliasing a namespace". 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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