Complete the code to register an autoloader function.
<?php spl_[1]('myAutoloader'); function myAutoloader($class) { include $class . '.php'; } ?>
The correct function to register an autoloader is spl_autoload_register.
Complete the autoloader to correctly load class files from the 'classes' directory.
<?php function myAutoloader($class) { include __DIR__ . '/[1]/' . $class . '.php'; } spl_autoload_register('myAutoloader');
The class files are stored in the 'classes' directory, so the autoloader must include that folder.
Fix the error in the autoloader function to avoid fatal errors when the file does not exist.
<?php function myAutoloader($class) { $file = __DIR__ . '/classes/' . $class . '.php'; if ([1]($file)) { include $file; } } spl_autoload_register('myAutoloader');
Using file_exists checks if the file is present before including it, preventing errors.
Fill both blanks to create an autoloader that supports namespaces by converting backslashes to directory separators.
<?php function myAutoloader($class) { $file = __DIR__ . '/classes/' . str_replace([1], [2], $class) . '.php'; if (file_exists($file)) { include $file; } } spl_autoload_register('myAutoloader');
Namespaces use backslashes '\\' which must be replaced with '/' for file paths.
Fill all three blanks to create a PSR-4 compliant autoloader that maps a base namespace to a base directory.
<?php function myAutoloader($class) { $prefix = [1]; $base_dir = __DIR__ . [2]; $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { return; } $relative_class = substr($class, $len); $file = $base_dir . str_replace([3], '/', $relative_class) . '.php'; if (file_exists($file)) { include $file; } } spl_autoload_register('myAutoloader');
The prefix is the base namespace 'App\\', the base directory is '/src/', and backslashes '\\' are replaced with slashes '/' in the relative class.