Refat Alsakka
Refat Alsakka

Reputation: 561

Do I need Autoloader-Class in PHP always?

I just start learning MVC in PHP, so my Question is

if I use PSR-4 for Autoload, why should I create Autoloader-Class or don't I need it anymore?

Upvotes: 0

Views: 225

Answers (1)

deceze
deceze

Reputation: 521995

PSR-4 is a specification that defines how to name your files and how autoloaders should load those files, so anyone can write "PSR-4 compliant" code and "PSR-4 compliant autoloaders" and they will work together, without having to reinvent the wheel for every individual library. Yes, you will still need an autoloader. PHP doesn't do autoloading out of the box, and just structuring your files according to PSR-4 doesn't make them autoload automagically.

The status quo is to use Composer to manage your dependencies, which comes with an autoloader which you can use. Or you use any other library management technique you want and any other autoloader you want, 3rd party library or one you have written yourself.

The alternative (to autoloading in general) is to write individual require_once ... statements, but that's a bit mad in this day and age and a bit of an unnecessary headache.

Upvotes: 3

Related Questions