0
0
PHPprogramming~20 mins

PSR-4 directory mapping in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PSR-4 Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
PSR-4 Namespace to Directory Mapping Output
Given the following PSR-4 autoload configuration and class usage, what will be the output when the script runs?
PHP
<?php
// composer.json autoload section
// "autoload": {
//     "psr-4": { "App\\": "src/" }
// }

// File: src/Utils/Helper.php
namespace App\Utils;

class Helper {
    public static function greet() {
        return "Hello from Helper!";
    }
}

// File: test.php
require 'vendor/autoload.php';

use App\Utils\Helper;

echo Helper::greet();
AFatal error: Class 'App\Utils\Helper' not found
BHello from Helper!\n
CParse error: syntax error, unexpected token
DHello from Helper!
Attempts:
2 left
💡 Hint
Remember PSR-4 maps namespaces to directories, so the class file must be in the correct folder matching the namespace.
🧠 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.
🔧 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.
📝 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.
🚀 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.