0
0
PHPprogramming~10 mins

Why autoloading is needed in PHP - Test Your Understanding

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

Complete the code to include a class file manually.

PHP
<?php
require_once '[1]';
$class = new MyClass();
?>
Drag options to blanks, or click blank then click option'
A"MyClass.php"
B"autoload.php"
C"config.php"
D"index.php"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated file names like 'config.php' or 'index.php'.
Forgetting to include the class file before using the class.
2fill in blank
medium

Complete the code to register an autoloader function.

PHP
<?php
spl_autoload_[1]('myAutoloader');
function myAutoloader($class) {
    include $class . '.php';
}
?>
Drag options to blanks, or click blank then click option'
Aload
Bautoload
Cregister
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'load', 'autoload', or 'include' which do not form the correct function name.
Using 'load' or 'include' which are not valid spl functions.
3fill in blank
hard

Fix the error in the autoloader function to correctly load class files.

PHP
<?php
spl_autoload_register(function($class) {
    include '[1]' . $class . '.php';
});
?>
Drag options to blanks, or click blank then click option'
A"/var/www/"
B"./"
C"classes/"
D"src/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or absolute paths that don't match file locations.
Forgetting the trailing slash in the path.
4fill in blank
hard

Fill both blanks to create an autoloader that loads classes from the 'lib' folder and appends '.class.php' extension.

PHP
<?php
spl_autoload_register(function($class) {
    include "[1]" . $class . "[2]";
});
Drag options to blanks, or click blank then click option'
A"lib/"
B".php"
C".class.php"
D"classes/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong folder names or missing the slash.
Using incorrect file extensions like '.php' instead of '.class.php'.
5fill in blank
hard

Fill all three blanks to create an autoloader that loads classes from 'src/', converts class names to lowercase, and appends '.php'.

PHP
<?php
spl_autoload_register(function($class) {
    $file = "[1]" . strtolower([2]) . "[3]";
    include $file;
});
Drag options to blanks, or click blank then click option'
A"src/"
B$class
C".php"
D"lib/"
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting class name to lowercase.
Using wrong folder or file extension.