Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple spl_autoload_register

Tags:

php

spl

autoload

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

example:

spl_autoload_register('autoload_systems');
spl_autoload_register('autoload_thirdparties');
spl_autoload_register('autoload_services');

vs:

using one

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

and then do the logic inside the function.

eg:
$ftp = new systems_ftp();
$services = new services_cron_email();
like image 610
Gino Sullivan Avatar asked Nov 20 '11 02:11

Gino Sullivan


1 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.

like image 136
alex Avatar answered Sep 21 '22 16:09

alex