What are the differences between __autoload and spl_autoload_register?

4

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>
asked by anonymous 16.07.2015 / 17:29

2 answers

6

The __autoload() function works as magic methods like __construct() , __destroy() , __string , etc.

__autoload () captures the name of an undeclared class. With the name of the class, just mount the "path" to load it. If the class has a declaration, __autoload() is ignored.

It is also possible to simplify the assembly of paths only by registering their bases with the function set_include_path() or ini_set('include_path','.') .

The disuse information is valid and is reported in the link: link

  

Tip spl_autoload_register () provides a more flexible alternative for   autoloading classes. For this reason, using __autoload () is   discouraged and may be deprecated or removed in the future.

The text already mentions the basic reason for discouraging the use of __autoload() and preference for the new spl_autoload_register() function. The spl_autoload_register() function provides more flexibility and improvements over __autoload() .

Invoking user-defined functions

The spl_autoload_register() function allows you to define custom classes for calling events.

class AiSeEu
{
   public static function TePego($class)
   {
         require $class . '.php';
   }
}

spl_autoload_register(['AiSeEu', 'TePego']);

In short, it allows multiple __autoloads () to be created. It may seem useless because it is almost the same as __autoload() , but this is useful when working with object-oriented programming.

Parameters as an anonymous function

spl_autoload_register(function ($class)
{
     return $class . '.php';
});

Just like any other PHP function, you can pass the parameters as an anonymous function. link

Include path

Note that, set_include_path() or ini_set('include_path','.') is also valid for functions invoked by spl_autoload_register() .

    
16.07.2015 / 18:09
3

__autoload is generally considered obsolete. It only allows a single automatic charger. Generally, you should only use __autoload if you are using a version of PHP not supported for spl_autload_register .

spl_autoload_register allows multiple autoloaders to be logged that will run until a match is found. This means that if you are using framework code or other third-party libraries that implement their own autoloaders you do not have to worry about possible conflicts caused by your autoload.

Original source: Soen

    
16.07.2015 / 18:09