Re: __autoload vs. spl_autoload_register
| From: | Gregory Beaver | Date: | Thu, 06 Sep 2007 23:11:28 +0000 |
| Subject: | Re: __autoload vs. spl_autoload_register | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-47933@lists.php.net to get a copy of this message | ||
Evert | Rooftop wrote:
> Hi List,
>
> I'm looking at the the pear autoloader in SVN
> (http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/trunk/src/Autoload.php?op=file&rev=0&sc=0)
>
>
> Wouldn't it make more sense to make use of spl_autoload_register
> instead? AFAIK this is a better practice because it allows multiple
> autoload handlers (for different libraries/directory schemes) possible.
>
> The normal __autoload also pollutes the global namespace, whereas
> spl_autoload_register does not. It is possible to provide a fallback, like:
>
> if (function_exists('__autoload')) spl_autoload_register('__autoload');
>
> The only drawback is that it is possible to disable the SPL library
> during PHP compilation, but I don't think any sensible person would do
> this and I'd say its fairly safe to have SPL as a dependency for PEAR.
Hi Evert,
Actually, I know several sensible people who do that. Eccentric, but
sensible :).
Normally I would agree with this whole-heartedly, and I am leaning
towards what you suggest, but it also adds complexity to a solution that
is designed only for absolute beginners/lazy people.
What might be an acceptable solution is:
if (function_exists('spl_autoload_register')) {
spl_autoload_register('PEAR2_Autoload');
return;
}
if (!function_exists('__autoload')) ....
What do you think?
Greg