PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
41: Autoloading with PSR-4
When I first started working with larger PHP projects, I had this nagging feeling that the vendor/autoload.php file was some sort of black box. I used to think that as long as I put my classes in a folder and called them the right name, PHP would just "figure it out." I figured the folder structure itself was the magic.
The "Magic Folder" Fallacy
Here is the mistake I see most often: a developer creates a directory called src/, drops a file called PaymentProcessor.php inside it, and then tries to instantiate it using a namespace like this:
// This will throw a Fatal Error: Class "App\Services\PaymentProcessor" not found
$payment = new \App\Services\PaymentProcessor();
The developer assumes that because the file is in the src folder, PHP knows it belongs to the App namespace. It doesn't. PHP has no native concept of "folders equal namespaces." Without a specific map telling PHP, "When you see the prefix App\, look inside the src/ directory," the engine is just guessing—and it's guessing wrong.
Mapping Namespaces to the File System
PSR-4 is not a piece of code; it's a standard. It's essentially a contract that says: "We agree that the namespace prefix will correspond to a specific base directory, and the rest of the namespace will correspond to the subdirectories."
To make this actually work, we use Composer. Instead of writing your own complex spl_autoload_register function (which I've done in the past, and trust me, it's a headache to maintain), you define the mapping in your composer.json file.
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
Notice the double backslash \\? That's just because the backslash is an escape character in JSON. This snippet tells Composer: "Any class starting with App\ can be found by looking in the src/ folder."
Now, if you want to use App\Services\PaymentProcessor, the PSR-4 standard dictates exactly where that file must live. It's not optional. The file must be at src/Services/PaymentProcessor.php, and the file must start with:
I'll give you a tip here: case sensitivity matters. On a Windows machine, you might get away with calling a file paymentprocessor.php (lowercase), but the moment you push that code to a Linux server, your app will crash. Always match your filename exactly to your class name.
The Final Handshake
Changing your composer.json isn't enough to trigger the change immediately. Composer generates a static map of these locations to keep things fast. Whenever you modify the autoload section, you have to run this in your terminal:
composer dump-autoload
Once you've done that and included require 'vendor/autoload.php'; at your entry point, you can stop worrying about require statements for every single class you write. You can just call the class, and Composer handles the file system hunt in the background.
📋 Practical Task
Implementing PSR-4 for a Library Catalog System
You are building a Library Management system. Currently, the project is a mess of require statements. Your goal is to convert the project to use PSR-4 autoloading via Composer.
The Goal: Configure the project so that the Lib\ namespace maps to the src/ directory.
Current File Structure:
composer.jsonindex.phpsrc/Books/Book.phpsrc/Members/Member.php
Your Tasks:
- Modify the
composer.jsonfile to add the PSR-4 autoload mapping for theLib\namespace pointing to thesrc/folder. - Open
src/Books/Book.phpand add the correctnamespacedeclaration so it matches the PSR-4 standard. - Open
src/Members/Member.phpand add the correctnamespacedeclaration. - In
index.php, remove anyrequirecalls to the individual class files and replace them with a singlerequire 'vendor/autoload.php';. - Instantiate both
Lib\Books\BookandLib\Members\Memberinindex.phpto verify it works.
There are no comments for now.