Composer - Autoload and PSR-0 vs PSR-4

Asked

Viewed 13,485 times

29

I’m starting to study Composer and am developing a system where I separate files from the core of the application files, as follows:

/ root 
    |-- /src 
         |-- /App 
               |-- /DBConfig
               |-- /Controller
               |-- /Model
         |-- /Core 
               |-- /Helper
               |-- /Controller
               |-- /Model

So, to set this configuration in the Composer.json file and get access to all classes of both /App and /Core, it would be this way?

"autoload" : {
    "psr-X" : {
        "App\\" : "/src",
        "Core\\" : "/src"
    }
}

Or is there a more correct way?

I’ve read, too, about PSR-0 vs PSR-4 and I’m still a bit in doubt which one to use. In my case which one I should implement, PSR-0 or PSR-4?

2 answers

28


The difference is that with the PSR-0 the autoloader will look for a folder with the name of the namespace you configured inside the directory and with the PSR-4 it will use the folder you set as the folder of the namespace itself.

Example PSR-0:

{
    "autoload": {
        "psr-0": {
            "Zend\\": "vendor"
        }
    }
}

The autoloader will look for the folder Zend, corresponding to the namespace Zend inside the briefcase vendor. Example, the namespace \Zend\Acl\Acl, will match the file vendor/Zend/Acl/Acl.php.

If you had set the path as vendor/Zend the file that the autoloader would try to load would be: vendor/Zend/Zend/Acl/Acl.php.

Example PSR-4:

{
    "autoload": {
        "psr-4": {
            "Zend\\": "vendor/Zend"
        }
    }
}

The autoloader will use the folder vendor/Zend like the namespace folder Zend, the namespace \Zend\Acl\Acl will match the file vendor/Zend/Acl/Acl.php.

Another example PSR-4:

{
    "autoload": {
        "psr-4": {
            "Zend\\": "algumacoisaqualquer/maisoutracoisaqualquer"
        }
    }
}

The autoloader will use the folder algumacoisaqualquer/maisoutracoisaqualquer like the namespace folder Zend, the namespace \Zend\Acl\Acl will match the file algumacoisaqualquer/maisoutracoisaqualquer/Acl/Acl.php.

Sources:

11

Goes from the situation. If the namespace goes to a folder where the first nomenclature is different, PSR-4. Example:

MyLib/
    LibComponents/
        Component1/
        Component2/
        ...

But the namespace of these Components, instead of starting with Mylib, start with Lib, so you specify in Composer:

{
    "autoload": {
        "psr-4": {
            "Lib\\": "vendor/some-lib/some-lib/libs/MyLib/"
        }
    }
}

I believe there is no more correct way, it goes from necessity.

In your case:

{
    "autoload": {
        "psr-4": {
            "App\\": "src/App/",
            "Core\\": "src/Core/"
        }
    }
}
  • Hmmm... I get it. So I think it would be better if you created an intermediate directory like: /src/Project/App/Controller/, then autoload would be just "Project ": "src/". What do you think?

  • It’s also a good one. By the way, I believe better than what you were doing before.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.