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
intermediate2: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 ?>
Attempts:
2 left
💡 Hint
Remember that manual include loads Foo.php before creating Foo, and autoload triggers for Bar class.
✗ Incorrect
The manual include loads Foo.php so Foo class is available. When Bar is instantiated, autoload triggers and includes Bar.php automatically, so both classes exist and their methods run correctly.
❓ Predict Output
intermediate2: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"; ?>
Attempts:
2 left
💡 Hint
Think about what happens when PHP tries to create an object of a class that was never included or autoloaded.
✗ Incorrect
Since Bar.php was never included and no autoload function is registered, PHP cannot find the Bar class and throws a fatal error.
🔧 Debug
advanced2: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 ?>
Attempts:
2 left
💡 Hint
Check how the filename is constructed in the autoload function and consider case sensitivity.
✗ Incorrect
The autoload function converts the class name to lowercase, so it tries to include 'lib/baz.php'. On case-sensitive systems, 'Baz.php' and 'baz.php' are different files, so include fails and class Baz is not found.
📝 Syntax
advanced2:00remaining
Identify the syntax error in autoload function
Which option contains the correct syntax for registering an autoload function in PHP?
Attempts:
2 left
💡 Hint
Remember the syntax for anonymous functions requires parentheses around parameters and braces around the body.
✗ Incorrect
Option A correctly uses parentheses around the parameter and braces around the function body. Option A misses parentheses around parameter, B misses semicolon inside braces, C misses braces.
🚀 Application
expert2: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 ?>
Attempts:
2 left
💡 Hint
Count manual includes plus autoload triggered includes for Bar and Baz classes.
✗ Incorrect
The script manually includes Foo.php. Then autoload triggers for Bar and Baz classes, including Bar.php and Baz.php. Total files included: 3.