A manual include is when you explicitly write code to load a PHP file using statements like include or require. You tell PHP exactly which file to load and when.
Click to reveal answer
beginner
What does autoloading do in PHP?
Autoloading automatically loads PHP classes when they are needed, without you writing <code>include</code> or <code>require</code> for each file. PHP calls a special function to find and load the class file on demand.
Click to reveal answer
intermediate
Name one advantage of autoloading over manual includes.
Autoloading reduces the need to write many include or require statements, making code cleaner and easier to maintain. It loads files only when needed, improving performance.
Click to reveal answer
intermediate
What PHP function is commonly used to register an autoloader?
The function spl_autoload_register() is used to register one or more autoload functions that PHP calls automatically to load classes.
Click to reveal answer
beginner
What happens if a manual include file is missing?
If you use require and the file is missing, PHP stops the script with a fatal error. If you use include, PHP shows a warning but continues running the script.
Click to reveal answer
Which PHP statement is used for manual file inclusion?
Ainclude
Bautoload
Cregister
Dloadfile
✗ Incorrect
The include statement is used to manually include PHP files.
What does spl_autoload_register() do?
AStops script execution
BIncludes a file manually
CRegisters a function to autoload classes automatically
DStarts a PHP session
✗ Incorrect
It registers a function that PHP calls to load classes automatically when needed.
Which is a benefit of autoloading over manual includes?
ASlows down the script
BLoads files only when classes are used
CRequires writing more include statements
DNeeds manual file path management
✗ Incorrect
Autoloading loads files only when the class is actually used, improving efficiency.
What happens if a required file is missing in manual include?
AScript stops with a fatal error
BScript continues silently
CPHP autoloads the file
DPHP ignores the error
✗ Incorrect
Using require causes a fatal error if the file is missing, stopping the script.
Which PHP feature helps avoid writing many include statements?
ACookies
BManual includes
CSessions
DAutoloading
✗ Incorrect
Autoloading automatically loads class files, reducing the need for manual includes.
Explain the difference between manual includes and autoloading in PHP.
Think about how files are loaded and when.
You got /4 concepts.
Describe one advantage and one disadvantage of manual includes compared to autoloading.
Consider ease of use and maintenance.
You got /3 concepts.
Practice
(1/5)
1. What is the main difference between manual includes and autoloading in PHP?
easy
A. Manual includes require you to write code to load each file, autoloading loads classes automatically.
B. Manual includes load classes automatically, autoloading requires manual file loading.
C. Manual includes are faster than autoloading in all cases.
D. Autoloading requires you to write code to include files manually.
Solution
Step 1: Understand manual includes
Manual includes require explicit code like include or require to load files.
Step 2: Understand autoloading
Autoloading automatically loads class files when you create an object, without explicit includes.
Final Answer:
Manual includes require you to write code to load each file, autoloading loads classes automatically. -> Option A
Quick Check:
Manual includes = explicit code, Autoloading = automatic loading [OK]
Hint: Manual includes need explicit code; autoloading is automatic [OK]
Common Mistakes:
Thinking autoloading needs manual include statements
Believing manual includes load classes automatically
Confusing speed differences between the two
2. Which of the following is the correct syntax to register an autoloader function in PHP?
easy
A. autoload_register('myLoader');
B. spl_autoload_register('myLoader');
C. register_autoload('myLoader');
D. spl_register_autoload('myLoader');
Solution
Step 1: Recall PHP autoload function
PHP uses spl_autoload_register to register autoload functions.
Step 2: Check syntax correctness
The correct function call is spl_autoload_register('myLoader'); where 'myLoader' is your function name.
Final Answer:
spl_autoload_register('myLoader'); -> Option B
Quick Check:
Correct autoload function = spl_autoload_register [OK]
Hint: Use spl_autoload_register to set autoload functions [OK]
Common Mistakes:
Using wrong function names like register_autoload
Mixing up function order or spelling
Forgetting to pass the function name as a string
3. Given the following code, what will be the output?
<?php
spl_autoload_register(function ($class) {
include $class . '.php';
});
$obj = new Car();
echo $obj->drive();
?>
Assuming Car.php exists and defines class Car with method drive() returning "Driving".
medium
A. Driving
B. Parse error
C. Nothing is printed
D. Fatal error: Class 'Car' not found
Solution
Step 1: Understand autoload behavior
The anonymous function registered with spl_autoload_register includes the file named after the class plus '.php'.
Step 2: Check class and method existence
Since Car.php exists and defines Car with drive() returning "Driving", the object creation and method call succeed.