0
0
PHPprogramming~20 mins

Common autoloading mistakes in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Autoloading 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 autoloading code?

Consider this PHP code using a simple autoloader. What will be the output when running this script?

PHP
<?php
spl_autoload_register(function ($class) {
    include $class . '.php';
});

$obj = new User();
echo $obj->getName();
?>
AOutputs the user's name if User.php exists and defines class User
BFatal error: Uncaught Error: Class 'User' not found
CWarning: include(User.php): failed to open stream: No such file or directory
DOutputs nothing and no error
Attempts:
2 left
💡 Hint

Think about what happens if the file User.php exists and defines the class User.

Predict Output
intermediate
2:00remaining
What error occurs with this autoloader?

What error will this PHP code produce when trying to instantiate Product?

PHP
<?php
spl_autoload_register(function ($class) {
    include 'classes/' . strtolower($class) . '.php';
});

$product = new Product();
?>
AParse error: syntax error
BFatal error: Uncaught Error: Class 'Product' not found
CNo error, object created successfully
DWarning: include(classes/product.php): failed to open stream: No such file or directory
Attempts:
2 left
💡 Hint

Check if the file path matches the class name case sensitivity.

🔧 Debug
advanced
2:30remaining
Why does this autoloader cause a fatal error?

Given this autoloader, why does instantiating Order cause a fatal error?

PHP
<?php
spl_autoload_register(function ($class) {
    require_once __DIR__ . '/src/' . $class . '.php';
});

$order = new Order();
?>
AThe file src/Order.php does not exist or path is incorrect
Brequire_once causes multiple inclusions leading to error
CThe class Order is defined with a namespace but autoloader ignores namespaces
DThe autoloader function is not registered properly
Attempts:
2 left
💡 Hint

Think about how namespaces affect file paths in autoloading.

📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in autoloader registration?

Which of these autoloader registrations will cause a syntax error?

Aspl_autoload_register(function $class { include $class . '.php'; });
Bspl_autoload_register(function($class) { include $class . '.php'; });
Cspl_autoload_register(fn($class) => include $class . '.php');
Dspl_autoload_register(function($class) { require_once $class . '.php'; });
Attempts:
2 left
💡 Hint

Check the syntax of anonymous functions in PHP.

🚀 Application
expert
3:00remaining
How many files will be included when instantiating these classes?

Given this autoloader and these class instantiations, how many files will be included?

spl_autoload_register(function ($class) {
    include_once __DIR__ . '/lib/' . str_replace('\\', '/', $class) . '.php';
});

$a = new App\Controller\Home();
$b = new App\Model\User();
$c = new App\Controller\Home();
A3 files included
B2 files included
C1 file included
DNo files included
Attempts:
2 left
💡 Hint

Consider how include_once works with repeated class instantiations.