List Info

Thread: __autoload vs. spl_autoload_register




__autoload vs. spl_autoload_register
user name
2007-09-06 17:09:48
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&re
v=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.

Evert

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


Re: __autoload vs. spl_autoload_register
user name
2007-09-06 18:11:28
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&re
v=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

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


Re: Re: __autoload vs. spl_autoload_register
user name
2007-09-06 18:35:32
On 9/6/07, Gregory Beaver <gregchiaraquartet.net>
wrote:
> 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&re
v=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
>
> --
> PEAR Development Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub
.php
>
>

Are you reading my files ? I am usually eccentric enough to
do such
thing. Paranoid ? Maybe. Safe ? Surely.

-- 
David Coallier,
Founder & Software Architect,
Agora Production (http://agoraproduction.com
)
51.42.06.70.18

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


Re: __autoload vs. spl_autoload_register
user name
2007-09-06 22:01:53
Gregory Beaver wrote:
> Evert | Rooftop wrote:
> 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?
>   

Thats actually ideal.. use the powertools when they are
available, otherwise fall back on the built in stuff..

How about:


if (function_exists('spl_autoload_register')) {
    spl_autoload_register('PEAR2_Autoload');
    if (function_exists('__autoload')) {
        spl_autoload_register('__autoload');
    }
} elseif (!function_exists('__autoload')) {
    function __autoload($class) {
       return PEAR2_Autoload($class);
    }
}

This will work in most cases, and still allow users to
define their own __autoload.

Evert
**

**

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )