Remember PSR-4 maps namespaces to directories, so the class file must be in the correct folder matching the namespace.
✗ Incorrect
The PSR-4 autoload maps the namespace 'App\\' to the 'src/' directory. The class Helper is in namespace 'App\\Utils', so it should be in 'src/Utils/Helper.php'. The autoloader loads the class correctly, so calling Helper::greet() outputs 'Hello from Helper!'.
🧠 Conceptual
intermediate
1:30remaining
PSR-4 Namespace Prefix and Directory Mapping
In PSR-4 autoloading, if the namespace prefix is "Vendor\\Package\\" mapped to the directory "lib/", where should the class Vendor\Package\Sub\MyClass be located?
Alib/Vendor/Package/Sub/MyClass.php
Blib/MyClass.php
Clib/Sub/MyClass.php
Dlib/Sub/MyClass.class.php
Attempts:
2 left
💡 Hint
PSR-4 removes the namespace prefix and replaces backslashes with directory separators inside the mapped directory.
✗ Incorrect
PSR-4 autoloading removes the namespace prefix 'Vendor\\Package\\' and maps the rest 'Sub\\MyClass' to 'Sub/MyClass.php' inside the 'lib/' directory.
🔧 Debug
advanced
2:30remaining
Why does this PSR-4 autoload succeed in finding the class?
Given this composer.json autoload section and file structure, why does the autoloader succeed to load the class Acme\Tools\Widget?
composer.json snippet:
{
"autoload": {
"psr-4": {
"Acme\\": "src/Acme/"
}
}
}
File structure:
- src/
- Acme/
- Tools/
- Widget.php
Widget.php content:
AThe namespace prefix 'Acme\\' maps to 'src/Acme/', so the class file should be at 'src/Acme/Tools/Widget.php', which is correct, so no error.
BThe autoloader requires the class file to be named 'Widget.class.php' instead of 'Widget.php'.
CThe class Widget is missing a leading backslash in the namespace declaration.
DThe namespace prefix 'Acme\\' should map to 'src/', not 'src/Acme/', so the autoloader looks for 'src/Acme/Acme/Tools/Widget.php' which does not exist.
Attempts:
2 left
💡 Hint
Check how the namespace prefix maps to the directory and how the rest of the namespace is appended.
✗ Incorrect
The prefix 'Acme\\' maps to 'src/Acme/', so for the class 'Acme\Tools\Widget' the autoloader strips 'Acme\\' leaving 'Tools\Widget', which becomes 'Tools/Widget.php' appended to 'src/Acme/' resulting in 'src/Acme/Tools/Widget.php'. This exactly matches the file structure, so the class loads successfully.
📝 Syntax
advanced
1:30remaining
Identify the PSR-4 compliant namespace and directory mapping
Which of the following composer.json autoload psr-4 mappings is correctly set up for the namespace MyApp\Core\ with classes stored in app/core/ directory?
A"MyApp\\Core": "app/core/"
B"MyApp\\Core\\": "app/core/"
C"MyApp\\Core\\": "app/core"
D"MyApp\\Core\\": "app\\core\\"
Attempts:
2 left
💡 Hint
PSR-4 requires the namespace prefix to end with double backslashes and directory path to end with a slash.
✗ Incorrect
Option B correctly uses double backslashes at the end of the namespace prefix and ends the directory path with a slash. Option B misses the trailing backslashes in the namespace prefix. Option B misses the trailing slash in the directory path. Option B uses backslashes in the directory path which is invalid in JSON and for PSR-4.
🚀 Application
expert
3:00remaining
Determine the number of classes autoloaded by PSR-4
Given this composer.json autoload section and directory structure, how many classes will be autoloaded correctly by PSR-4?
composer.json snippet:
{
"autoload": {
"psr-4": {
"Lib\\": "lib/",
"Lib\\Utils\\": "lib/utils/"
}
}
}
Directory structure:
- lib/
- Foo.php (namespace Lib; class Foo)
- Bar.php (namespace Lib; class Bar)
- utils/
- Helper.php (namespace Lib\Utils; class Helper)
- Extra.php (namespace Lib\Utils; class Extra)
- Old.php (namespace Lib; class Old)
Which classes will be autoloaded correctly?
AFoo, Bar, Helper, and Extra only
BHelper, Extra, and Old only
CFoo, Bar, Helper, Extra, and Old
DFoo, Bar, and Old only
Attempts:
2 left
💡 Hint
PSR-4 uses the longest matching namespace prefix to map classes to directories.
✗ Incorrect
The prefix 'Lib\\Utils\\' maps to 'lib/utils/', so Helper and Extra are loaded from there. The prefix 'Lib\\' maps to 'lib/', so Foo and Bar are loaded from 'lib/'. The class Old is in namespace 'Lib' but located in 'lib/utils/Old.php' which does not match the 'Lib\\' prefix directory, so it will not be autoloaded correctly.
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?