Bird
Raised Fist0
PHPprogramming~20 mins

Composer autoload mechanism in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Composer's autoload mechanism in PHP?
easy
A. To automatically load PHP classes without manual includes
B. To compile PHP code into binary files
C. To create database connections automatically
D. To minify PHP scripts for faster loading

Solution

  1. Step 1: Understand Composer's role

    Composer manages dependencies and helps load classes automatically.
  2. Step 2: Identify autoload mechanism purpose

    Autoloading means PHP classes are loaded when needed without manual includes.
  3. Final Answer:

    To automatically load PHP classes without manual includes -> Option A
  4. Quick Check:

    Autoload = automatic class loading [OK]
Hint: Autoload means no manual includes needed [OK]
Common Mistakes:
  • Thinking autoload compiles code
  • Confusing autoload with database tasks
  • Assuming autoload minifies scripts
2. Which file must you include in your PHP script to use Composer's autoload feature?
easy
A. autoload_classmap.php
B. composer.json
C. vendor/autoload.php
D. composer.lock

Solution

  1. Step 1: Identify the autoload file

    Composer generates vendor/autoload.php to load all classes automatically.
  2. Step 2: Differentiate from other files

    composer.json and composer.lock manage dependencies but don't autoload classes directly.
  3. Final Answer:

    vendor/autoload.php -> Option C
  4. Quick Check:

    Include vendor/autoload.php to autoload classes [OK]
Hint: Always include vendor/autoload.php for autoloading [OK]
Common Mistakes:
  • Including composer.json instead of autoload.php
  • Trying to include composer.lock file
  • Confusing autoload_classmap.php as main include
3. Given the following PHP code, what will be the output if Composer's autoload is correctly set up and the class App\Utils\Helper exists?
require 'vendor/autoload.php';
use App\Utils\Helper;
$helper = new Helper();
echo $helper->greet();
medium
A. Fatal error: Class 'App\Utils\Helper' not found
B. Hello from Helper!
C. Parse error: syntax error
D. Nothing will be printed

Solution

  1. Step 1: Check autoload inclusion

    The code includes vendor/autoload.php, enabling class autoloading.
  2. Step 2: Confirm class usage and method call

    The class App\Utils\Helper is used and instantiated, then greet() is called.
  3. Final Answer:

    Hello from Helper! -> Option B
  4. Quick Check:

    Autoload loads class, greet() outputs greeting [OK]
Hint: If autoload works, class methods run as expected [OK]
Common Mistakes:
  • Forgetting to include vendor/autoload.php
  • Misspelling the namespace or class name
  • Assuming no output without checking method
4. What is the error in this PHP code snippet using Composer autoload?
require 'vendor/autoload.php';
use App\Services\Mailer;
$mailer = new Mailer();
$mailer->sendEmail('test@example.com');

Assuming the class App\Services\Mailer exists but the code throws a fatal error.
medium
A. The namespace or class name is misspelled
B. The sendEmail method does not exist in Mailer class
C. vendor/autoload.php file is missing or not included
D. The Mailer class is not autoloaded because composer dump-autoload was not run

Solution

  1. Step 1: Check autoload file inclusion

    If vendor/autoload.php is missing or path is wrong, classes won't load.
  2. Step 2: Understand fatal error cause

    Fatal error usually means class not found, often due to missing autoload file or autoload not updated.
  3. Final Answer:

    The Mailer class is not autoloaded because composer dump-autoload was not run -> Option D
  4. Quick Check:

    Not running composer dump-autoload causes class not found error [OK]
Hint: Check vendor/autoload.php inclusion first on fatal errors [OK]
Common Mistakes:
  • Assuming method missing without checking class load
  • Ignoring autoload file path correctness
  • Blaming namespace spelling without verifying autoload
5. You want to autoload your own PHP classes located in the src/ folder using Composer. Which step correctly configures Composer to autoload these classes using PSR-4 standard?
hard
A. Add "autoload": {"psr-4": {"App\\": "src/"}} in composer.json and run composer dump-autoload
B. Add "autoload": {"classmap": ["src/"]} in composer.json and run composer install
C. Manually include all files in src/ in your PHP scripts
D. Add "autoload": {"files": ["src/"]} in composer.json and run composer update

Solution

  1. Step 1: Understand PSR-4 autoloading

    PSR-4 maps namespaces to folder paths, e.g., "App\\" to "src/".
  2. Step 2: Configure composer.json and regenerate autoload files

    Add the correct autoload section and run composer dump-autoload to update.
  3. Final Answer:

    Add "autoload": {"psr-4": {"App\\": "src/"}} in composer.json and run composer dump-autoload -> Option A
  4. Quick Check:

    PSR-4 + composer dump-autoload = correct autoload setup [OK]
Hint: Use PSR-4 in composer.json and run dump-autoload [OK]
Common Mistakes:
  • Using classmap or files instead of PSR-4 for namespaces
  • Forgetting to run composer dump-autoload after changes
  • Manually including files instead of using autoload
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