📖 Scenario: You are building a simple PHP project with multiple classes. You want to understand the difference between manually including class files and using autoloading to load classes automatically.
🎯 Goal: Create a PHP script that first uses manual include statements to load class files, then refactor it to use an autoloader function. You will see how autoloading simplifies your code.
📋 What You'll Learn
Create two PHP class files with exact class names and methods
Manually include these class files in a main script
Create an autoloader function using spl_autoload_register
Use the autoloader to instantiate classes without manual includes
Print messages from class methods to show successful loading
💡 Why This Matters
🌍 Real World
In real PHP projects, autoloading helps manage many class files without writing many include statements. It keeps code clean and easier to maintain.
💼 Career
Understanding autoloading is essential for PHP developers working on modern applications and frameworks like Laravel or Symfony.
Progress0 / 4 steps
1
Create two PHP class files
Create two PHP files named Car.php and Bike.php. In Car.php, define a class Car with a method drive() that returns the string "Car is driving". In Bike.php, define a class Bike with a method ride() that returns the string "Bike is riding".
PHP
Hint
Each class should be in its own file named exactly Car.php and Bike.php.
2
Manually include class files in main script
Create a PHP file named index.php. Use include statements to include Car.php and Bike.php. Then create objects $car of class Car and $bike of class Bike.
PHP
Hint
Use include 'Car.php'; and include 'Bike.php'; at the top of index.php.
3
Add autoloader function to load classes automatically
In index.php, remove the manual include statements. Instead, add an autoloader function using spl_autoload_register that loads class files by appending .php to the class name. Then create objects $car and $bike as before.
PHP
Hint
The autoloader function should include the file named after the class with .php extension.
4
Print messages from class methods to show loading works
In index.php, add echo statements to print the results of $car->drive() and $bike->ride() on separate lines.
PHP
Hint
Use echo $car->drive() . "\n"; and echo $bike->ride() . "\n"; to print messages.
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.