M
M
microf2019-04-18 18:02:21
Composer
microf, 2019-04-18 18:02:21

Composer autoload. What have I done wrong?

How does composer autoload work?
Folders
-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 { }

Here doctor.php (Doctor class) works (connected from index.php), but RegisterDoctorServices is gone. Issues
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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OnYourLips, 2019-04-18
@OnYourLips

Are there PSR-4 mappings in composer.json?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question