PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] Composer autoload not found classes psr4

 January 17, 2022     autoload, composer-php, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing