PHP - Namespaces and Composer Autoloader

Here is the first of a series of videos on Refactoring. Specifically, in this video we are going to focus on using namespaces following PSR-4 and how this is structured at the autoloader level (taking advantage of Composer's own) and the IDE (adjusting the preferences of PhpStorm). Below I leave the list of topics covered and the times of the video as well as the related material:

Topics Covered

  • Transition from a custom autoloader to using the Composer autoloader
  • Applying namespaces following the PSR-4 standard
  • Optimization of the classmap generated by Composer through the configuration of composer.json
  • Project configuration in PhpStorm to generate the correct namespaces based on the folder structure
  • Moving source files to the src/ folder

Video

Index

  • 0:00 - Current state of the code
  • 3:50 - What is a global namespace and why is it necessary
  • 5:24 - Creating the "src/" folder and configuring PhpStorm to recognize the new global namespace
  • 6:45 - Moving all classes to the new "src/" folder
  • 8:15 - Modifying the classes to introduce the namespace
  • 11:12 - Removing the custom autoloader that was included in the project
  • 11:40 - Modifying the composer.json file to include loading policy in order to properly generate the autoloader
    Autoloader Composer with PSR-4
  • 13:05 - Specification of the optimization of the classmap generated by Composer through the configuration of composer.json
    Optimize composer autoloader by configuration
  • 14:35 - Demonstration of class creation through PhpStorm with automatic generation of the correct namespace
  • 15:58 - Recap and conclusion of the process carried out

Related Material

Upcoming Videos

Pay according to your needs

lite (only monthly)

19 €
per month
  • Access to a subset of courses to lay the foundation for maintainable, scalable, and testable code
  • Company invoice
Popular

standard

24,92 €
Save 121
Annual payment of 299
per month
  • Full course catalog
  • Design and architecture challenges
  • Highlighted solutions videos for challenges
  • Receive job offers verified by Codely
  • Company invoice

premium

41,58 €
Save 89
Annual payment of 499
per month
  • Everything above
  • More benefits coming soon

We won’t increase the price while your subscription is active