Understanding Autoloading (PSR-4) - What is the Benefit of using it

Posted 4 years ago by fsdolphin

Hi,

Can someone explain what the main benefit is by using PSR-4?

I was watching Jeffrey's Namespacing and Autoloading video and I don't really see the benefit, I'm pretty sure I'm missing something but so far I don't see that being faster or easier than manually importing the classes with require, in fact I feel like there is more steps when using Autoloading and at the end I see that you end up with the same import lines.

Using Autoload:

  1. Create Composer json file
    composer.json file
  1. Namespace your classes namespace Acme;
  1. Pull projects in index.php
    require 'vendor/autoload.php';
    require 'ex.php'
  1. Import classes in ex.php file
    use Acme\Person;
    use Acme\Business
    use Acme\Staff;

Vs

Manually:

require 'src/Person.php';
require 'src/Business.php';
require 'src/Staff.php';

I don't know but I just don't see the advantage of using Autoloaders, PSR-4 on this case. If the main benefit is namespacing than I'm ok but I was under the impression that this should Autoload your classes without too much effort. Sorry if I just don't see the obvious but having a little experience with other languages such as C++, Objective-C, Swift etc. where I always imported my classes manually I don't see the benefit.

I'm pretty sure I'm just not seeing the big picture here.

REF: https://laracasts.com/series/object-oriented-bootcamp-in-php/episodes/6

Thanks

Please sign in or create an account to participate in this conversation.