What are the differences between __autoload and spl_autoload_register?

Asked

Viewed 4,997 times

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)?

2 answers

7


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

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

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

The disused information is valid and is reported on the link: http://php.net/manual/en/language.oop5.autoload.php

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 role spl_autoload_register(). The function spl_autoload_register() provides more flexibility and improvements in relation to __autoload().

Invoking user-defined functions

The function spl_autoload_register() 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 pointless because it is practically the same result as the __autoload(), however, this becomes useful when working with object-oriented programming.

Parameters as 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. http://php.net/manual/en/functions.anonymous.php

Include path

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

  • 1

    +1 for information, -1 for Teló :)

  • Aiseeu::Tepego($class); throw new Naopegounadaexception('error caused in Michel Teló')

  • It makes you want to edit this question, because of this Michel Teló

4

__autoload is generally considered obsolete. It only allows a single auto loader. Generally, you should only use __autoload if you are using a version of PHP without support 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 your own autoloaders you don’t have to worry about possible conflicts caused by your autoload.

Original source: Soen

  • great complement. I forgot to mention about conflict prevention

Browser other questions tagged

You are not signed in. Login or sign up in order to post.