V
V
Vladislav Olegovich2020-10-18 16:52:19
PHP
Vladislav Olegovich, 2020-10-18 16:52:19

Why doesn't PHP see the parent class when using the Composer class autoloader?

I have a project, here is its structure:
5f8c45af6dd5e456589909.png
I, accordingly, used Composer to autoload classes, I registered this in cmposer.json:

"autoload":
    {
        "psr-4":
        {
            "core\\": "core",
            "application\\controllers\\": "controllers",
            "application\\models\\": "models",
            "application\\views\\": "views"
        }
    }

Don't forget to click composer install .
I have a folder models , which contains 2 files: model.php and authmodel.php ,
respectively, these are 2 classes, the contents of model.php :
namespace application\models;
require_once __DIR__.'/../../vendor/autoload.php';

class Model
{

}


And the content of authmodel.php :
namespace application\models;
require_once __DIR__.'/../../vendor/autoload.php';

class AuthModel extends Model
{

}


As you can see, they are in the same folder, they have Composer's autoloader connected, they are in the same namespace, but when I use AuthModel , I get an error:

Fatal error: Class 'application\models\Model' not found in D:\prog\openserv\OpenServer\domains\test.dev\application\models\authmodel.php on line 7

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2020-10-18
@delphinpro

1. Character case matters.
2. You don't need to include an autoloader in every file. This is done once, at the entry point.
3. Why so much garbage in the autoloader configuration?
It is enough to specify the root namespace.

"psr-4":
        {
            "core\\": "core",
            "application\\": "application",
        }

here I explained in detail how to use the composer to autoload PSR-0 or PSR-4, and how to properly build the project structure?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question