Consider this PHP code using a simple autoloader. What will be the output when running this script?
<?php spl_autoload_register(function ($class) { include $class . '.php'; }); $obj = new User(); echo $obj->getName(); ?>
Think about what happens if the file User.php exists and defines the class User.
If the file User.php exists and contains the class User, the autoloader includes it and the object is created successfully. Then getName() outputs the user's name.
What error will this PHP code produce when trying to instantiate Product?
<?php spl_autoload_register(function ($class) { include 'classes/' . strtolower($class) . '.php'; }); $product = new Product(); ?>
Check if the file path matches the class name case sensitivity.
The autoloader tries to include 'classes/product.php' but if the actual file is named 'Product.php' with uppercase P, it will fail on case-sensitive filesystems.
Given this autoloader, why does instantiating Order cause a fatal error?
<?php spl_autoload_register(function ($class) { require_once __DIR__ . '/src/' . $class . '.php'; }); $order = new Order(); ?>
Think about how namespaces affect file paths in autoloading.
If the class Order is namespaced, the autoloader must convert namespace separators to directory separators. Ignoring namespaces causes the file not to be found.
Which of these autoloader registrations will cause a syntax error?
Check the syntax of anonymous functions in PHP.
Option A misses parentheses around the parameter list in the anonymous function, causing a syntax error.
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();Consider how include_once works with repeated class instantiations.
The autoloader includes 'lib/App/Controller/Home.php' once and 'lib/App/Model/User.php' once. The second instantiation of Home does not include the file again due to include_once.