In php, we have two methods of doing autoload
of classes:
function __autoload
Example:
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
function spl_autoload_register
Example:
class Autoloader
{
public static function load($class)
{
require $class . '.php';
}
}
spl_autoload_register(['Autoloader', 'load']);
// ou
spl_autoload_register(function ($class)
{
return $class . '.php';
});
Of course, at first we see that the differences are in the statement: One, you have to declare a function called __autoload, and another, you use an (already existing) function called spl_autoload_register
.
However, the PHP Manual does not recommend using __autoload
, because in future versions it could be removed (actually, I do not know if I have this warning in the manual anymore, because I did not find it there when I read the __autoload
again).
Thus:
-
What are the differences between the two?
- li>