0
0
PHPprogramming~20 mins

Manual includes vs autoloading in PHP - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Manual Includes and Autoloading
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of manual include vs autoloading
What will be the output of this PHP code snippet that uses manual includes and autoloading?
PHP
<?php
// File: classes/Foo.php

// File: autoload.php
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.php';
});

include 'classes/Foo.php';
$foo1 = new Foo();
echo $foo1->sayHello() . "\n";

$foo2 = new Bar();
echo $foo2->sayHello() . "\n";

// File: classes/Bar.php
?>
AHello from Bar!\nHello from Foo!\n
BHello from Foo!\nFatal error: Uncaught Error: Class 'Bar' not found
CHello from Foo!\nHello from Bar!\n
DFatal error: Uncaught Error: Class 'Foo' not found
Attempts:
2 left
💡 Hint
Remember that manual include loads Foo.php before creating Foo, and autoload triggers for Bar class.
Predict Output
intermediate
2:00remaining
What error occurs without autoloading?
What error will this PHP code produce if autoloading is not set up and only Foo.php is manually included?
PHP
<?php
include 'classes/Foo.php';
$foo = new Foo();
echo $foo->sayHello() . "\n";
$bar = new Bar();
echo $bar->sayHello() . "\n";
?>
AHello from Foo!\nHello from Bar!\n
BFatal error: Uncaught Error: Class 'Bar' not found
CFatal error: Uncaught Error: Class 'Foo' not found
DParse error: syntax error, unexpected 'Bar' (T_STRING)
Attempts:
2 left
💡 Hint
Think about what happens when PHP tries to create an object of a class that was never included or autoloaded.
🔧 Debug
advanced
2:00remaining
Why does autoload fail to load class?
Given this autoload function, why does instantiating class Baz fail with a fatal error?
PHP
<?php
spl_autoload_register(function ($class) {
    include 'lib/' . strtolower($class) . '.php';
});

$baz = new Baz();
echo $baz->greet();

// File: lib/Baz.php
?>
AThe class Baz is not defined in the included file.
BThe autoload function is missing a return statement.
CThe spl_autoload_register function is called after the class instantiation.
DThe filename is Baz.php but autoload tries to include baz.php, which does not exist (case-sensitive filesystem).
Attempts:
2 left
💡 Hint
Check how the filename is constructed in the autoload function and consider case sensitivity.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in autoload function
Which option contains the correct syntax for registering an autoload function in PHP?
Aspl_autoload_register(function ($class) { include 'classes/' . $class . '.php'; });
Bspl_autoload_register(function ($class) { include 'classes/' . $class . '.php' });
Cspl_autoload_register(function($class) include 'classes/' . $class . '.php';);
Dspl_autoload_register(function $class { include 'classes/' . $class . '.php'; });
Attempts:
2 left
💡 Hint
Remember the syntax for anonymous functions requires parentheses around parameters and braces around the body.
🚀 Application
expert
2:00remaining
How many files are included during execution?
Consider this PHP script with manual includes and autoloading. How many files are included when the script runs?
PHP
<?php
include 'classes/Foo.php';
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.php';
});
$foo = new Foo();
$bar = new Bar();
baz();
function baz() {
    $baz = new Baz();
    echo $baz->sayHello();
}
// Files:
// classes/Foo.php defines class Foo
// classes/Bar.php defines class Bar
// classes/Baz.php defines class Baz
?>
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Count manual includes plus autoload triggered includes for Bar and Baz classes.