The PHP OOP Autoload method helps automatically load class files when they are needed, instead of including each file manually.
spl_autoload_register()The __autoload() function was used to automatically load undefined classes. However, it is deprecated since PHP 7.2.0.
Instead, spl_autoload_register() allows registering multiple autoload functions safely.
// Register an autoload function for loading class files
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
// Creating an object triggers automatic loading of User.php
$obj = new User();
When the User class is instantiated, PHP automatically searches for User.php and includes it without manual intervention.
This mechanism behaves like an on-demand loader — classes are fetched only when required, reducing memory usage.
spl_autoload_register()Product, Order, and Customer classesinclude