Home » Php » php – multiple spl_autoload_register

php – multiple spl_autoload_register

Posted by: admin April 23, 2020 Leave a comment


what is/are the benefit(s) of having multiple spl_autoload_register




using one

spl_autoload_register('autoload'); or __autoload();

and then do the logic inside the function.

$ftp = new systems_ftp();
$services = new services_cron_email();
How to&Answers:

If you have one __autoload() and you include a third party library which also had one, it would be clobbered.

Registering multiple autoloads with spl_autoload_register() ensures you can have your code dropped in with existing code (think libraries, etc) without clobbering or shadowing of existing/future autoloads.

In addition, the PHP manual states

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.


i guess no need to add multiple but its on your logic.
the way spl_autoload_register works is awesome.
suppone one have 3rd parties directries and that is ussually manages through namespace which also represents their path.
consider this simple class as autoloader class

class MyAutoLoader {

public function __construct()
    spl_autoload_register( array($this, 'load') );

function load( $class )

    $filepath = 'classes/'.$class.'.php';
    require_once( $filepath);



then in index file include this loader file and create instance of this class

$autoload = new MyAutoLoader();

now u can directly create the instances of classes
suppose we have ‘Person’ class in directory ‘classes\QMS\SERVICES\Person.php’ and same path is the namespace of that class

$person = new QMS\SERVICES\Person();

now when when you will use new it will check if this class exists and value as a string will be passes to ‘load’ function of class ‘MyAutoLoader’. and there it will get included. or u can change the function ‘load’ to fix the path of ur class files put ur if conditions or whatever need to b done to fix the paths..