Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer - Autoload and PSR-0 vs PSR-4

I'm beginning to study Composer and am developing a system where I separate the files core application files, as follows:

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

So, to set this setting in composer.json file and get access to all classes both /App much /Core would be this way?


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

Or is there a more correct way?

I have also read about PSR-0 vs PSR-4 and I am still somewhat in doubt which one to use. In my case what should I implement, PSR-0 or PSR-4?

like image 922
LeoFelipe Avatar asked Jun 04 '14 19:06

LeoFelipe


1 Answers

You didn't need 2 entries just one for the main namespace so something like this for PSR-4:

    "autoload" : {
        "psr-4" : {
            "MyApp\\" : "/src"            }
    }

As long as everything in src/ uses the same namespace that's all you'll need. Just let the autoloader do it's job.

As to which to use I'd go with PSR-4 because at some point it is expected that PSR-0 will be deprecated and as PSR-4 is made to be backwards compatible minus some warts for older legacy programs there isn't really a difference except of you start using some of it newer features

like image 199
Dragonaire Avatar answered Sep 30 '22 09:09

Dragonaire