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
- 13:05 - Specification of the optimization of the classmap generated by Composer through the configuration of composer.json
- 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
- Specification of the PSR-4 standard for PHP namespaces from PHP FIG
- Examples of PSR-4 implementation
- How to use PSR-4
- Explanation and benchmarks on optimizing Composer's autoloader
- Optimize-autoloader directive of composer.json
- Documentation on PhpStorm directory settings
Upcoming Videos
- PHP - Code Style, PSR-1 and PSR-2 Standard (Basic Level)
- SOLID - Single Responsibility Principle and Interface Segregation Principle (Medium Level)
- SOLID - Dependency Inversion Principle (Medium Level)
- DTOs / Anemic Domain Models vs Domain Models (Medium Level)