Monday, January 17, 2022

[FIXED] Composer autoload not found classes psr4

Issue

I try to use composer to load custom classes which are located in specific folder.

Here is my project strucure

public
|—index.php
src
|—classes
    |— MyClass
          |— MyClass.php
vendor
|—autoload.php
composer.json

Index.php

<?php

namespace ink;

require ( vendor/autoload.php );

use ink\src\classes\MyClass\MyClass;

$customclasse = new MyClass();

MyClass.php

<?php

namespace ink\src\classes\MyClass;

class MyClass {

     public function __construct(){
     }
}

composer.json

{
    autoload : {
        psr-4 : {
            ink\\ : src 
        }
    }
}

What I’m doing wrong ? Namespaces seems good cause when I require MyClass.php everything works fine, class is loaded.

Thanks


Solution

You are doubling up a src level.

By ink\\ : src you are saying that anything after ink is in the src folder.

But your class is in a ink\src\classes\MyClass namespaces.

So this adds up to src+src/classes/MyClass=src/src/classes/MyClass path.

So you likely need:

{
    autoload : {
        psr-4 : {
            ink\\src\\ : src 
        }
    }
}


Answered By - Rarst

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.