Как работает composer autoload?
Папки
-src
--doctor
---domain
----entity (Doctor.php)
----services (RegisterDoctorServices.php)
index.php
use Doctor\Entity\Doctor;
try {
require_once __DIR__ . './vendor/autoload.php';
new Doctor();
} catch (Error $e) {
echo $e;
}
composer.json
{
"autoload": {
"psr-4": {
"Doctor\\": "./src/doctor/domain"
}
}
}
index.php
use Doctor\Entity\Doctor;
try {
require_once __DIR__ . './vendor/autoload.php';
new Doctor();
} catch (Error $e) {
echo $e;
}
Doctor.php
namespace Doctor\Entity;
use Doctor\Services\RegisterDoctorServices;
class Doctor {
public function __construct() {
echo 'Doctor works!';
new RegisterDoctorServices();
}
}
RegisterDoctorServices.php
<?php
namespace Doctor\Services;
class RegisterDoctorServices { }
Вот doctor.php (класс Doctor) работает (подключается из index.php), а RegisterDoctorServices уже нет. Выдает
Doctor works!
Fatal error: Uncaught Error: Class 'Doctor\Services\RegisterDoctorServices' not found in D:\OSPanel\domains\ddd\src\doctor\domain\entity\Doctor.php:54 Stack trace: #0 D:\OSPanel\domains\ddd\index.php(14): Doctor\Entity\Doctor->__construct() #1 {main} thrown in D:\OSPanel\domains\ddd\src\doctor\domain\entity\Doctor.php on line 54