PSR-4 directory mapping in PHP - Time & Space Complexity
Let's see how the time to find a PHP class file grows when using PSR-4 directory mapping.
We want to know how the file lookup time changes as the number of classes grows.
Analyze the time complexity of the following PSR-4 autoloader snippet.
function loadClass($className) {
$prefix = 'App\\';
$baseDir = __DIR__ . '/src/';
if (str_starts_with($className, $prefix)) {
$relativeClass = substr($className, strlen($prefix));
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require $file;
}
}
}
This code maps a class name to a file path and loads it if found.
Look for repeated steps when loading classes.
- Primary operation: Checking if the class name starts with the prefix and converting namespace separators to directory separators.
- How many times: Once per class load request.
The time to load a class depends on how long it takes to build the file path and check if the file exists.
| Input Size (number of classes) | Approx. Operations |
|---|---|
| 10 | 10 file path builds and checks |
| 100 | 100 file path builds and checks |
| 1000 | 1000 file path builds and checks |
Pattern observation: Each class load requires a direct path build and file check, so operations grow linearly with the number of classes loaded.
Time Complexity: O(1)
This means loading a single class file takes about the same time regardless of how many classes exist, because the path is computed directly.
[X] Wrong: "Loading a class gets slower as more classes are added because it searches through all files."
[OK] Correct: PSR-4 uses a direct mapping from class name to file path, so it does not search all files but directly checks one file.
Understanding how PSR-4 autoloading works and its time complexity shows you know how modern PHP apps efficiently load code on demand.
What if the autoloader had to scan multiple directories for each class? How would the time complexity change?
Practice
PSR-4 helps with: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 AQuick Check:
PSR-4 = Namespace to folder mapping [OK]
- Confusing PSR-4 with coding style rules
- Thinking PSR-4 encrypts code
- Assuming PSR-4 manages databases
composer.json?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.Final Answer:
"autoload": { "psr-4": { "App\\": "src/" } } -> Option CQuick Check:
Namespace ends with \\ and folder path uses / [OK]
- Using single backslash in JSON string
- Mixing slashes in folder paths
- Omitting trailing backslash in namespace
composer.json snippet:{
"autoload": {
"psr-4": {
"MyApp\\": "app/"
}
}
}Where should the class
MyApp\Controllers\HomeController be located?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 DQuick Check:
Namespace after prefix = Controllers/HomeController.php [OK]
- Adding namespace prefix folder again
- Using wrong base folder like src/
- Wrong file extension
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?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 runcomposer dump-autoloadto update the autoloader.Final Answer:
Composer dump-autoload was not run after adding the class -> Option AQuick Check:
Run dump-autoload after changes [OK]
- Changing folder slashes incorrectly
- Removing trailing backslash in namespace prefix
- Forgetting to update autoloader
"App\\": "src/",
"Vendor\\Package\\": "vendor/package/src/"Which
composer.json snippet correctly sets this up?Solution
Step 1: Check namespace keys format
Namespace keys must end with double backslashes to separate namespaces properly.Step 2: Check folder paths format
Folder paths use forward slashes and end with a slash for directories.Step 3: Validate each option
"autoload": { "psr-4": { "App\\": "src/", "Vendor\\Package\\": "vendor/package/src/" } } correctly uses double backslashes for both namespaces and trailing slashes for folders.Final Answer:
"autoload": { "psr-4": { "App\\": "src/", "Vendor\\Package\\": "vendor/package/src/" } } -> Option BQuick Check:
Double backslashes + trailing slash folder paths [OK]
- Omitting trailing backslash in namespace keys
- Using forward slashes in namespace keys
- Missing trailing slash in folder paths
Ready to prove it?
Pass the test and this pattern moves unknown
