PSR-4 is a standard for autoloading PHP classes from file paths that match their namespaces. It helps PHP find and load classes automatically based on their namespace and class name.
Click to reveal answer
beginner
How does PSR-4 map namespaces to directories?
PSR-4 maps a namespace prefix to a base directory. The namespace parts after the prefix become subdirectories inside that base directory, and the class name becomes the file name with a .php extension.
Click to reveal answer
beginner
Example: If the namespace prefix is App\ mapped to src/, where is the class App\Controllers\HomeController located?
The class App\Controllers\HomeController is located at src/Controllers/HomeController.php.
Click to reveal answer
intermediate
Why is PSR-4 preferred over older autoloading methods?
PSR-4 is preferred because it is simple, standardized, and avoids conflicts. It makes code easier to organize and share by following a clear rule to find classes based on namespaces.
Click to reveal answer
intermediate
What happens if a class file does not follow PSR-4 directory mapping?
If a class file does not follow PSR-4 mapping, the autoloader will not find the file automatically, causing errors like "class not found". You must then load the file manually or fix the directory structure.
Click to reveal answer
What does PSR-4 autoloading map to find a class file?
AFunction name to URL path
BClass name to database table
CNamespace prefix to base directory
DVariable name to environment variable
✗ Incorrect
PSR-4 maps a namespace prefix to a base directory to locate class files.
If the namespace is MyApp\Models\User and prefix MyApp\ maps to lib/, where is the class file?
Alib/User.php
Blib/MyApp/Models/User.php
CModels/User.php
Dlib/Models/User.php
✗ Incorrect
The namespace after prefix becomes subdirectories: Models/User.php inside lib/.
What file extension does PSR-4 expect for class files?
A.php
B.txt
C.inc
D.class
✗ Incorrect
PSR-4 expects PHP class files to have the .php extension.
What error occurs if PSR-4 mapping is incorrect?
AClass not found error
BSyntax error
CMemory overflow
DDivision by zero
✗ Incorrect
Incorrect mapping causes "class not found" errors because the autoloader can't find the file.
Which of these is NOT a benefit of PSR-4?
AStandardized autoloading
BAutomatically compiles PHP to machine code
CSimplifies class loading
DAvoids naming conflicts
✗ Incorrect
PSR-4 does not compile PHP to machine code; it only standardizes autoloading.
Explain how PSR-4 directory mapping works in PHP autoloading.
Think about how namespaces relate to folder paths.
You got /4 concepts.
Describe a common error when PSR-4 directory mapping is not followed and how to fix it.
Consider what happens if the autoloader can't find the file.
You got /4 concepts.
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?