0
0
PHPprogramming~10 mins

Manual includes vs autoloading in PHP - Interactive Practice

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

Complete the code to manually include the file 'User.php'.

PHP
<?php
[1] 'User.php';
$user = new User();
?>
Drag options to blanks, or click blank then click option'
Aload
Binclude
Crequire_once
Dautoload
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'autoload' which is not a PHP statement for manual includes.
Using 'load' which is not a valid PHP keyword.
2fill in blank
medium

Complete the code to register an autoloader function named 'myAutoloader'.

PHP
<?php
spl_autoload_[1]('myAutoloader');
?>
Drag options to blanks, or click blank then click option'
Aregister
Binclude
Cautoload
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' or 'load' which are not functions to register autoloaders.
Using 'spl_register' which is not a valid PHP function.
3fill in blank
hard

Fix the error in the autoloader function to correctly load class files from the 'classes/' directory.

PHP
<?php
function myAutoloader($className) {
    include_once 'classes/' . [1] . '.php';
}
spl_autoload_register('myAutoloader');
?>
Drag options to blanks, or click blank then click option'
A$class_name
B$classname
CclassName
D$className
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the function parameter.
Forgetting the dollar sign ($) before the variable.
4fill in blank
hard

Fill both blanks to create an autoloader that loads classes from the 'lib/' directory and uses the class name with '.class.php' extension.

PHP
<?php
function [1]($class) {
    include_once 'lib/' . $class . [2];
}
spl_autoload_register('[1]');
Drag options to blanks, or click blank then click option'
AmyAutoloader
B.php
C.class.php
Dautoload
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.php' instead of '.class.php' for the file extension.
Using a different function name than the one registered.
5fill in blank
hard

Fill all three blanks to create an autoloader that loads classes from the 'src/' directory, converts class names to lowercase, and uses '.inc.php' extension.

PHP
<?php
function [1]($className) {
    $file = 'src/' . strtolower([2]) . [3];
    if (file_exists($file)) {
        include_once $file;
    }
}
spl_autoload_register('[1]');
Drag options to blanks, or click blank then click option'
AautoLoadClass
B$className
C'.inc.php'
Dstrtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable without the dollar sign.
Using wrong file extension or function name.