0
0
PHPprogramming~20 mins

Composer autoload mechanism in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composer Autoload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Composer autoload usage?

Given the following PHP code that uses Composer's autoload, what will be the output?

require 'vendor/autoload.php';

use App\Utils\Greeting;

$greet = new Greeting();
echo $greet->sayHello();

Assume the class App\Utils\Greeting is correctly defined in src/Utils/Greeting.php and Composer's autoload is configured with PSR-4 for the App\ namespace.

PHP
require 'vendor/autoload.php';

use App\Utils\Greeting;

$greet = new Greeting();
echo $greet->sayHello();
AFatal error: Class 'App\Utils\Greeting' not found
BHello from Greeting!
CWarning: require(vendor/autoload.php): failed to open stream
DParse error: syntax error, unexpected 'use' (T_USE)
Attempts:
2 left
💡 Hint

Think about how Composer's autoload maps namespaces to file paths.

🧠 Conceptual
intermediate
1:00remaining
Which file does Composer generate for autoloading classes?

Composer generates a file that handles autoloading of classes based on your composer.json configuration. What is the name and location of this file?

Acomposer/autoload.php
Bautoload.php in the project root
Csrc/autoload.php
Dvendor/autoload.php
Attempts:
2 left
💡 Hint

Look inside the vendor directory after running composer install.

🔧 Debug
advanced
2:30remaining
Why does this Composer autoload fail to load the class?

Consider this composer.json snippet:

{
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

And this PHP code:

require 'vendor/autoload.php';

use App\Controllers\HomeController;

$controller = new HomeController();

The file src/Controllers/HomeController.php exists and defines the class App\Controllers\HomeController. However, running the code causes a Class not found error. What is the most likely cause?

AThe require statement path is wrong and does not include autoload.php
BThe namespace in HomeController.php is incorrect and does not match App\Controllers
CComposer's autoload files were not regenerated after adding the class file
DThe class HomeController is declared as abstract and cannot be instantiated
Attempts:
2 left
💡 Hint

Think about what Composer needs to do after adding new classes.

📝 Syntax
advanced
1:30remaining
What error does this Composer autoload code produce?

What error will this PHP code produce?

require 'vendor/autoload.php'

use App\Models\User;

$user = new User();
AParse error: syntax error, unexpected 'use' (T_USE)
BFatal error: Class 'App\Models\User' not found
CWarning: require(vendor/autoload.php): failed to open stream
DNo error, outputs nothing
Attempts:
2 left
💡 Hint

Check the syntax carefully, especially punctuation.

🚀 Application
expert
3:00remaining
How many classes will be autoloaded with this Composer config?

Given this composer.json autoload section:

{
  "autoload": {
    "psr-4": {
      "Lib\\": "lib/",
      "Lib\\Utils\\": "lib/Utils/"
    }
  }
}

And the following files exist:

  • lib/Helper.php (class Lib\Helper)
  • lib/Utils/Parser.php (class Lib\Utils\Parser)
  • lib/Utils/Formatter.php (class Lib\Utils\Formatter)
  • lib/Utils/Extra/Tool.php (class Lib\Utils\Extra\Tool)

How many of these classes will be autoloaded correctly by Composer?

A4
B1
C3
D2
Attempts:
2 left
💡 Hint

Remember how PSR-4 prefixes work and how they map to directories.