5
In php, we have two methods of making one autoload
class:
__autoload function
Example:
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
spl_autoload_register function
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 the other, you use an (existing) function called spl_autoload_register
.
However, the PHP Handbook does not recommend using __autoload
, because in future versions it could be removed (actually, I don’t know if there’s any more of that warning in the manual, because I didn’t find it there when I read the manual of __autoload
again).
So:
What are the differences between the two?
Why the use of
spl_autoload_register
is (or was) encouraged, rather than__autoload
(since at first,__autoload
seems easier to use)?
+1 for information, -1 for Teló :)
– bfavaretto
Aiseeu::Tepego($class); throw new Naopegounadaexception('error caused in Michel Teló')
– Wallace Maxters
It makes you want to edit this question, because of this Michel Teló
– Wallace Maxters