📖 Scenario: You are building a simple PHP project that follows the PSR-4 standard for autoloading classes. This standard helps PHP know where to find your classes based on their namespace and class name.Imagine you have a folder called src where all your PHP classes live, and you want to set up the autoloader so PHP can automatically load your classes without you having to include files manually.
🎯 Goal: Set up a PSR-4 autoloader mapping in PHP that links the namespace MyApp\ to the src/ directory. Then, create a class inside the src/Utils folder and use the autoloader to load and instantiate it.
📋 What You'll Learn
Create an associative array called $psr4 that maps the namespace prefix 'MyApp\\' to the directory 'src/'.
Create a variable called $className with the full class name 'MyApp\\Utils\\Helper'.
Write code to convert the class name to a file path using the PSR-4 rules and store it in $filePath.
Print the value of $filePath to show the mapped file location.
💡 Why This Matters
🌍 Real World
PSR-4 autoloading is a standard used by most modern PHP projects and frameworks to load classes automatically without manual includes.
💼 Career
Understanding PSR-4 is essential for PHP developers working with frameworks like Laravel, Symfony, or Composer packages.
Progress0 / 4 steps
1
Create the PSR-4 namespace to directory mapping
Create an associative array called $psr4 that maps the namespace prefix 'MyApp\\' to the directory 'src/'.
PHP
Hint
Remember to escape backslashes in PHP strings by doubling them.
2
Set the full class name to load
Create a variable called $className and set it to the string 'MyApp\\Utils\\Helper'.
PHP
Hint
Use single quotes and double backslashes for namespaces.
3
Convert the class name to a file path using PSR-4 rules
Write code to convert the class name in $className to a file path using the PSR-4 rules and store it in a variable called $filePath. Use the $psr4 array to find the base directory. Replace the namespace prefix with the directory, and replace backslashes with directory separators, then add .php at the end.
PHP
Hint
Use substr to remove the prefix and str_replace to change backslashes to slashes.
4
Print the mapped file path
Write a print statement to display the value of $filePath.
PHP
Hint
Use print($filePath); to show the result.
Practice
(1/5)
1. What is the main purpose of PSR-4 in PHP? PSR-4 helps with:
easy
A. Mapping namespaces to directory paths for autoloading classes
B. Defining coding style rules for PHP files
C. Encrypting PHP source code for security
D. Managing database connections automatically
Solution
Step 1: Understand PSR-4 role
PSR-4 is a standard that maps PHP namespaces to folder paths to load classes automatically.
Step 2: Compare options
Only Mapping namespaces to directory paths for autoloading classes describes this mapping and autoloading purpose correctly.
Final Answer:
Mapping namespaces to directory paths for autoloading classes -> Option A
Quick Check:
PSR-4 = Namespace to folder mapping [OK]
Hint: PSR-4 links namespaces to folders for autoloading [OK]
Common Mistakes:
Confusing PSR-4 with coding style rules
Thinking PSR-4 encrypts code
Assuming PSR-4 manages databases
2. Which of the following is the correct way to define a PSR-4 autoload entry in composer.json?
easy
A. "autoload": { "psr-4": { "App": "src" } }
B. "autoload": { "psr-4": { "App/": "src\\" } }
C. "autoload": { "psr-4": { "App\\": "src/" } }
D. "autoload": { "psr-4": { "App\\": "src\\" } }
Solution
Step 1: Check PSR-4 syntax in composer.json
PSR-4 keys use double backslashes for namespace separator and forward slashes for folder paths.
Step 2: Validate each option
"autoload": { "psr-4": { "App\\": "src/" } } correctly uses "App\\" for namespace and "src/" for directory path.
Where should the class MyApp\Controllers\HomeController be located?
medium
A. app/Controllers/HomeController.class.php
B. app/MyApp/Controllers/HomeController.php
C. src/Controllers/HomeController.php
D. app/Controllers/HomeController.php
Solution
Step 1: Understand PSR-4 mapping
The namespace prefix "MyApp\\" maps to folder "app/". The rest of the namespace after prefix maps to subfolders.
Step 2: Map class namespace to file path
Class "MyApp\\Controllers\\HomeController" maps to "app/Controllers/HomeController.php".
Final Answer:
app/Controllers/HomeController.php -> Option D
Quick Check:
Namespace after prefix = Controllers/HomeController.php [OK]
Hint: Namespace after prefix = folder path + class name.php [OK]
Common Mistakes:
Adding namespace prefix folder again
Using wrong base folder like src/
Wrong file extension
4. You have this composer.json autoload section:
{
"autoload": {
"psr-4": {
"Lib\\": "lib/"
}
}
}
But when you try to use Lib\Utils\Helper, PHP cannot find the class. What is the likely problem?
medium
A. Composer dump-autoload was not run after adding the class
B. The class file is not located at lib/Utils/Helper.php
C. The namespace prefix "Lib\\" should not have trailing backslash
D. The folder path should be "lib\\" with double backslashes
Solution
Step 1: Check common PSR-4 issues
Folder path uses forward slashes, trailing backslash in namespace prefix is correct, so options C and D are wrong.
Step 2: Verify autoload update
After adding new classes, you must run composer dump-autoload to update the autoloader.
Final Answer:
Composer dump-autoload was not run after adding the class -> Option A
Quick Check:
Run dump-autoload after changes [OK]
Hint: Always run composer dump-autoload after adding classes [OK]
Common Mistakes:
Changing folder slashes incorrectly
Removing trailing backslash in namespace prefix
Forgetting to update autoloader
5. You want to autoload two separate namespaces in your project: "App\\": "src/",
"Vendor\\Package\\": "vendor/package/src/" Which composer.json snippet correctly sets this up?