0
0
PHPprogramming~10 mins

Common autoloading mistakes in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to register an autoloader function.

PHP
<?php
spl_[1]('myAutoloader');
function myAutoloader($class) {
    include $class . '.php';
}
?>
Drag options to blanks, or click blank then click option'
Aregister
Binclude
Cautoload
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using spl_register instead of spl_autoload_register
Using spl_load which does not exist
Using spl_include which is invalid
2fill in blank
medium

Complete the autoloader to correctly load class files from the 'classes' directory.

PHP
<?php
function myAutoloader($class) {
    include __DIR__ . '/[1]/' . $class . '.php';
}
spl_autoload_register('myAutoloader');
Drag options to blanks, or click blank then click option'
Aclasses
Blib
Csrc
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong folder names like 'src' or 'lib' when files are in 'classes'
Forgetting to add the directory separator '/'
3fill in blank
hard

Fix the error in the autoloader function to avoid fatal errors when the file does not exist.

PHP
<?php
function myAutoloader($class) {
    $file = __DIR__ . '/classes/' . $class . '.php';
    if ([1]($file)) {
        include $file;
    }
}
spl_autoload_register('myAutoloader');
Drag options to blanks, or click blank then click option'
Ais_readable
Bis_dir
Cis_file
Dfile_exists
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_dir which checks for directories, not files
Using is_readable without checking existence first
Not checking at all, causing fatal errors
4fill in blank
hard

Fill both blanks to create an autoloader that supports namespaces by converting backslashes to directory separators.

PHP
<?php
function myAutoloader($class) {
    $file = __DIR__ . '/classes/' . str_replace([1], [2], $class) . '.php';
    if (file_exists($file)) {
        include $file;
    }
}
spl_autoload_register('myAutoloader');
Drag options to blanks, or click blank then click option'
A'\\'
B'/'
C'\\\'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single backslash without escaping
Replacing with wrong characters
Not escaping backslash properly
5fill in blank
hard

Fill all three blanks to create a PSR-4 compliant autoloader that maps a base namespace to a base directory.

PHP
<?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');
Drag options to blanks, or click blank then click option'
A'App\\'
B'/src/'
C'\\'
D'App\'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single backslash in prefix without escaping
Wrong base directory path
Not replacing backslashes properly