Bird
Raised Fist0
PHPprogramming~20 mins

Manual includes vs autoloading in PHP - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Manual Includes and Autoloading
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of manual include vs autoloading
What will be the output of this PHP code snippet that uses manual includes and autoloading?
PHP
<?php
// File: classes/Foo.php

// File: autoload.php
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.php';
});

include 'classes/Foo.php';
$foo1 = new Foo();
echo $foo1->sayHello() . "\n";

$foo2 = new Bar();
echo $foo2->sayHello() . "\n";

// File: classes/Bar.php
?>
AHello from Bar!\nHello from Foo!\n
BHello from Foo!\nFatal error: Uncaught Error: Class 'Bar' not found
CHello from Foo!\nHello from Bar!\n
DFatal error: Uncaught Error: Class 'Foo' not found
Attempts:
2 left
💡 Hint
Remember that manual include loads Foo.php before creating Foo, and autoload triggers for Bar class.
❓ Predict Output
intermediate
2:00remaining
What error occurs without autoloading?
What error will this PHP code produce if autoloading is not set up and only Foo.php is manually included?
PHP
<?php
include 'classes/Foo.php';
$foo = new Foo();
echo $foo->sayHello() . "\n";
$bar = new Bar();
echo $bar->sayHello() . "\n";
?>
AHello from Foo!\nHello from Bar!\n
BFatal error: Uncaught Error: Class 'Bar' not found
CFatal error: Uncaught Error: Class 'Foo' not found
DParse error: syntax error, unexpected 'Bar' (T_STRING)
Attempts:
2 left
💡 Hint
Think about what happens when PHP tries to create an object of a class that was never included or autoloaded.
🔧 Debug
advanced
2:00remaining
Why does autoload fail to load class?
Given this autoload function, why does instantiating class Baz fail with a fatal error?
PHP
<?php
spl_autoload_register(function ($class) {
    include 'lib/' . strtolower($class) . '.php';
});

$baz = new Baz();
echo $baz->greet();

// File: lib/Baz.php
?>
AThe class Baz is not defined in the included file.
BThe autoload function is missing a return statement.
CThe spl_autoload_register function is called after the class instantiation.
DThe filename is Baz.php but autoload tries to include baz.php, which does not exist (case-sensitive filesystem).
Attempts:
2 left
💡 Hint
Check how the filename is constructed in the autoload function and consider case sensitivity.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in autoload function
Which option contains the correct syntax for registering an autoload function in PHP?
Aspl_autoload_register(function ($class) { include 'classes/' . $class . '.php'; });
Bspl_autoload_register(function ($class) { include 'classes/' . $class . '.php' });
Cspl_autoload_register(function($class) include 'classes/' . $class . '.php';);
Dspl_autoload_register(function $class { include 'classes/' . $class . '.php'; });
Attempts:
2 left
💡 Hint
Remember the syntax for anonymous functions requires parentheses around parameters and braces around the body.
🚀 Application
expert
2:00remaining
How many files are included during execution?
Consider this PHP script with manual includes and autoloading. How many files are included when the script runs?
PHP
<?php
include 'classes/Foo.php';
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.php';
});
$foo = new Foo();
$bar = new Bar();
baz();
function baz() {
    $baz = new Baz();
    echo $baz->sayHello();
}
// Files:
// classes/Foo.php defines class Foo
// classes/Bar.php defines class Bar
// classes/Baz.php defines class Baz
?>
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Count manual includes plus autoload triggered includes for Bar and Baz classes.

Practice

(1/5)
1. What is the main difference between manual includes and autoloading in PHP?
easy
A. Manual includes require you to write code to load each file, autoloading loads classes automatically.
B. Manual includes load classes automatically, autoloading requires manual file loading.
C. Manual includes are faster than autoloading in all cases.
D. Autoloading requires you to write code to include files manually.

Solution

  1. Step 1: Understand manual includes

    Manual includes require explicit code like include or require to load files.
  2. Step 2: Understand autoloading

    Autoloading automatically loads class files when you create an object, without explicit includes.
  3. Final Answer:

    Manual includes require you to write code to load each file, autoloading loads classes automatically. -> Option A
  4. Quick Check:

    Manual includes = explicit code, Autoloading = automatic loading [OK]
Hint: Manual includes need explicit code; autoloading is automatic [OK]
Common Mistakes:
  • Thinking autoloading needs manual include statements
  • Believing manual includes load classes automatically
  • Confusing speed differences between the two
2. Which of the following is the correct syntax to register an autoloader function in PHP?
easy
A. autoload_register('myLoader');
B. spl_autoload_register('myLoader');
C. register_autoload('myLoader');
D. spl_register_autoload('myLoader');

Solution

  1. Step 1: Recall PHP autoload function

    PHP uses spl_autoload_register to register autoload functions.
  2. Step 2: Check syntax correctness

    The correct function call is spl_autoload_register('myLoader'); where 'myLoader' is your function name.
  3. Final Answer:

    spl_autoload_register('myLoader'); -> Option B
  4. Quick Check:

    Correct autoload function = spl_autoload_register [OK]
Hint: Use spl_autoload_register to set autoload functions [OK]
Common Mistakes:
  • Using wrong function names like register_autoload
  • Mixing up function order or spelling
  • Forgetting to pass the function name as a string
3. Given the following code, what will be the output?
<?php
spl_autoload_register(function ($class) {
    include $class . '.php';
});

$obj = new Car();
echo $obj->drive();
?>
Assuming Car.php exists and defines class Car with method drive() returning "Driving".
medium
A. Driving
B. Parse error
C. Nothing is printed
D. Fatal error: Class 'Car' not found

Solution

  1. Step 1: Understand autoload behavior

    The anonymous function registered with spl_autoload_register includes the file named after the class plus '.php'.
  2. Step 2: Check class and method existence

    Since Car.php exists and defines Car with drive() returning "Driving", the object creation and method call succeed.
  3. Final Answer:

    Driving -> Option A
  4. Quick Check:

    Autoload loads Car.php, drive() returns "Driving" [OK]
Hint: Autoload includes file; method returns string output [OK]
Common Mistakes:
  • Assuming class not found error without autoload
  • Forgetting file must exist with correct class
  • Expecting no output if method returns string
4. What is the error in this code snippet?
<?php
function myLoader($class) {
    include 'classes/' . $class . '.php';
}
spl_autoload_register('myLoader');

$obj = new User();
?>
Assuming the file classes/User.php does not exist.
medium
A. No error, object created successfully
B. Warning: spl_autoload_register expects a callable
C. Syntax error in function declaration
D. Fatal error: Class 'User' not found

Solution

  1. Step 1: Analyze autoload function behavior

    The autoload function tries to include classes/User.php when creating User object.
  2. Step 2: Check file existence

    Since classes/User.php does not exist, the include fails (warning) and the class is not loaded, causing a fatal error: Class 'User' not found.
  3. Final Answer:

    Fatal error: Class 'User' not found -> Option D
  4. Quick Check:

    Missing file causes fatal class not found error [OK]
Hint: Missing file causes fatal class not found error [OK]
Common Mistakes:
  • Assuming no error if file missing
  • Confusing fatal error with warning
  • Thinking spl_autoload_register needs different syntax
5. You have many classes in different folders and want to use autoloading efficiently. Which approach is best to handle this scenario?
hard
A. Use multiple spl_autoload_register calls with random file paths.
B. Manually include every class file at the start of your script.
C. Write a single autoload function that maps namespaces to folder paths and includes files accordingly.
D. Avoid autoloading and use require_once for each class.

Solution

  1. Step 1: Understand autoloading with namespaces

    Using namespaces mapped to folder paths allows a single autoload function to locate and include class files efficiently.
  2. Step 2: Compare with manual includes and multiple autoloaders

    Manual includes are tedious and error-prone; multiple random autoloaders cause confusion and inefficiency.
  3. Final Answer:

    Write a single autoload function that maps namespaces to folder paths and includes files accordingly. -> Option C
  4. Quick Check:

    Namespace mapping in autoload = best practice [OK]
Hint: Map namespaces to folders in one autoload function [OK]
Common Mistakes:
  • Including all files manually for many classes
  • Registering multiple unrelated autoloaders
  • Ignoring namespaces and folder structure
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