Bird
Raised Fist0
PHPprogramming~10 mins

PSR-4 directory mapping in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the base namespace prefix for PSR-4 autoloading.

PHP
$prefix = "[1]\\";
Drag options to blanks, or click blank then click option'
Aautoload
Bsrc
Cvendor
DApp
Attempts:
3 left
💡 Hint
Common Mistakes
Using folder names like 'src' or 'vendor' as namespace prefix.
Including backslashes in the prefix string.
2fill in blank
medium

Complete the code to map the namespace prefix to the directory path in PSR-4.

PHP
$baseDir = __DIR__ . '/[1]/';
Drag options to blanks, or click blank then click option'
Alib
Bbin
Csrc
Dapp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bin' or 'lib' which may not contain PHP classes.
Using absolute paths instead of relative paths.
3fill in blank
hard

Fix the error in the autoloader function to correctly replace namespace prefix with directory path.

PHP
if (strncmp($class, $prefix, [1]) !== 0) {
    return;
}
Drag options to blanks, or click blank then click option'
Astrlen($prefix)
Bstrlen($class)
Cstrpos($class, $prefix)
Dstrlen($baseDir)
Attempts:
3 left
💡 Hint
Common Mistakes
Using length of directory path instead of prefix.
Using strpos which returns position, not length.
4fill in blank
hard

Fill both blanks to correctly build the file path from the class name.

PHP
$relativeClass = substr($class, [1]);
$file = $baseDir . str_replace([2], '/', $relativeClass) . '.php';
Drag options to blanks, or click blank then click option'
Astrlen($prefix)
B'\\'
C'/'
Dstrlen($baseDir)
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing forward slashes instead of backslashes.
Using length of base directory instead of prefix.
5fill in blank
hard

Fill all three blanks to complete the PSR-4 autoloader registration code.

PHP
spl_autoload_register(function ($class) {
    $prefix = '[1]\\';
    $baseDir = __DIR__ . '/[2]/';
    if (strncmp($class, $prefix, [3]) !== 0) {
        return;
    }
});
Drag options to blanks, or click blank then click option'
AApp
Bsrc
Cstrlen($prefix)
Dlib
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong directory names.
Not using the length of prefix in strncmp.

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

  1. Step 1: Understand PSR-4 role

    PSR-4 is a standard that maps PHP namespaces to folder paths to load classes automatically.
  2. Step 2: Compare options

    Only Mapping namespaces to directory paths for autoloading classes describes this mapping and autoloading purpose correctly.
  3. Final Answer:

    Mapping namespaces to directory paths for autoloading classes -> Option A
  4. 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

  1. Step 1: Check PSR-4 syntax in composer.json

    PSR-4 keys use double backslashes for namespace separator and forward slashes for folder paths.
  2. Step 2: Validate each option

    "autoload": { "psr-4": { "App\\": "src/" } } correctly uses "App\\" for namespace and "src/" for directory path.
  3. Final Answer:

    "autoload": { "psr-4": { "App\\": "src/" } } -> Option C
  4. Quick Check:

    Namespace ends with \\ and folder path uses / [OK]
Hint: Use double backslashes for namespaces, forward slashes for folders [OK]
Common Mistakes:
  • Using single backslash in JSON string
  • Mixing slashes in folder paths
  • Omitting trailing backslash in namespace
3. Given this composer.json snippet:
{
  "autoload": {
    "psr-4": {
      "MyApp\\": "app/"
    }
  }
}

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

  1. Step 1: Understand PSR-4 mapping

    The namespace prefix "MyApp\\" maps to folder "app/". The rest of the namespace after prefix maps to subfolders.
  2. Step 2: Map class namespace to file path

    Class "MyApp\\Controllers\\HomeController" maps to "app/Controllers/HomeController.php".
  3. Final Answer:

    app/Controllers/HomeController.php -> Option D
  4. 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

  1. 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.
  2. Step 2: Verify autoload update

    After adding new classes, you must run composer dump-autoload to update the autoloader.
  3. Final Answer:

    Composer dump-autoload was not run after adding the class -> Option A
  4. 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?
hard
A. "autoload": { "psr-4": { "App/": "src/", "Vendor/Package/": "vendor/package/src/" } }
B. "autoload": { "psr-4": { "App\\": "src/", "Vendor\\Package\\": "vendor/package/src/" } }
C. "autoload": { "psr-4": { "App\\": "src/", "Vendor\\Package": "vendor/package/src" } }
D. "autoload": { "psr-4": { "App": "src", "Vendor\\Package\\": "vendor/package/src/" } }

Solution

  1. Step 1: Check namespace keys format

    Namespace keys must end with double backslashes to separate namespaces properly.
  2. Step 2: Check folder paths format

    Folder paths use forward slashes and end with a slash for directories.
  3. 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.
  4. Final Answer:

    "autoload": { "psr-4": { "App\\": "src/", "Vendor\\Package\\": "vendor/package/src/" } } -> Option B
  5. Quick Check:

    Double backslashes + trailing slash folder paths [OK]
Hint: Use double backslashes for namespaces and trailing slash for folders [OK]
Common Mistakes:
  • Omitting trailing backslash in namespace keys
  • Using forward slashes in namespace keys
  • Missing trailing slash in folder paths
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard