30

hello is there any diffrence useing this excepts that we can use our own name auto load? is there any performance difference? how do they internally work?

between

function __autoload_libraries($class){
    include_once 'lib.'.$class.'.php';
}

spl_autoload_register('__autoload_libraries');

vs

function __autoload($class){
    include_once 'lib.'.$class.'.php';
}
0

2 Answers 2

56

__autoload is generally considered obsolete. It only allows for a single autoloader. Generally you should only use __autoload if you're using a version of PHP without support for spl_autload_register.

spl_autoload_register allows several autoloaders to be registered which will be run through in turn until a matching class/interface/trait is found and loaded, or until all autoloading options have been exhausted. This means that if you're using framework code or other third party libraries that implement their own autoloaders you don't have to worry about yours causing conflicts.

UPDATE:

__autoload is now officially deprecated as of PHP 7.2.0, which means it's now on the chopping block. If you want your code to be compatible with future versions of PHP you definitely should not use __autoload

2
  • 1
    i cant see any good example doing this on php.net/manual/en/function.spl-autoload-register.php, when i tried it, it loads the other autoload two, after the file from autoload one got included. Aug 1, 2011 at 5:55
  • To me it seems like __autoload() could be much more convenient than spl_autoload_register() if used inside a NS or class, not just globally. That way the autoload function running would always know from which NS it was invoked and could easily handle a hierarchical folder structure for local/shared/core classes... Some aspects of this was way easier with old style NS_Class naming and multiple include paths for root than it is with composer.. :/ Oct 8, 2021 at 19:38
3

Documentation for "Autoloading Classes" reads:

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.

Documentation for spl_autoload_register() reads:

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.