RE: [PHP-DEV] Re: [PHP-WEBMASTER] Why isn't `spl_autoload` called for functions?
| From: | Laupretre François | Date: | Wed, 07 Nov 2012 13:15:08 +0000 |
| Subject: | RE: [PHP-DEV] Re: [PHP-WEBMASTER] Why isn't `spl_autoload` called for functions? | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-63774@lists.php.net to get a copy of this message | ||
OK, sorry, as I hadn't read the 'autofunc' RFC, I was not talking about the API
described there. You're right: by introducing a second argument to spl_autoload_register(), you
register each autoloader with the type it can be called for.
The API I was talking about would not modify spl_autoload_register(), but would check at runtime
wether each autoloader is 'new-style' or 'old-style' by checking the number of
args it declares. This way we can restrict calls to old-style autoloaders to 'class'
resolutions only.
Both APIs are technically OK. The only potential problem I see with the autofunc API is that it is
incompatible with the __autoload() way to declare an autoloader. I don't say we must keep
__autoload() AND spl_autoload_register() but it is a BC break.
> -----Message d'origine-----
> De : sebastian.krebs.berlin@gmail.com
> [mailto:sebastian.krebs.berlin@gmail.com] De la part de Sebastian Krebs
> Envoyé : mercredi 7 novembre 2012 09:12
> À : PHP internals list
> Objet : Re: [PHP-DEV] Re: [PHP-WEBMASTER] Why isn't
>
spl_autoload
> called for functions?
>
> 2012/11/7 Laupretre François <francois.laupretre@francetv.fr>
>
> > > De : sebastian.krebs.berlin@gmail.com
> > > > I hope you find more support on this than I had. Technically
> > > speaking,
> > > > it would be easy to extend autoloading to functions and
> constants.
> > > > It can even be done without BC breaks, combining 'old-style' and
> > > > 'new-
> > > style'
> > > > autoloaders without ambiguity (adding a second argument to the
> > > > autoload hook, and checking the number of args declared by the
> > > function).
> > > >
> > >
> > > Thats not even required, if you fixate (--> default value) the
> > > second parameter to "class" all existing loaders will behave, like
> > > they did before.
> >
> > Correct, but that's not enough to preserve BC.
> >
> > OK, giving a default value of 'class' to the second arg allows using
> a
> > 'new-style' autoloader with an 'old-style' PHP engine.
> >
> > But, now consider the opposite, using a 'new-style' PHP engine, with
> > autoloading extended to functions and constants, with one or more
> > 'old-style' autoloaders: when the engine needs to autoload a function
> > or a constant, we must not call any 'old-style' autoloader, as it
> > would take the symbol and attempt to load a class by that name. And
> > the easiest way to decide if an autoloader is a new or old-style one
> > is to check if the autoload handler func is declared with one or two
> args.
> >
>
> // signature
> function spl_autoload_register($autoload, $type = SPL_AUTOLOAD_CLASS);
>
> // "Old style"
> spl_autoload_register(function($classname) {
> // foobar
> });
>
> // "new style"
> sply_autoload_register(function($name, $type) {
> switch ($type) {
> // Foobar
> }
> }, SPL_AUTOLOAD_CLASS | SPL_AUTOLOAD_FUNCTION | SPL_AUTOLOAD_CONSTANT);
>
> Why is this "not enough"? Even if we pass the type as second argument
> to the autoloader, nothing (in behaviour) will change. You are right,
> that we must not call the first one, when it's not required, but also
> it _will_ not get called, because it is not registered to get called
> for anything except classes. Maybe I got you wrong?
>
>
> If you want a more "organized" autoloader, create a class for it :)
>
> class MyAutoloader {
> public function __invoke ($name, $type) {
> switch ($type) {
> case SPL_AUTOLOAD_CLASS: return $this->loadClass($name); break;
> case SPL_AUTOLOAD_FUNCTION: return $this->loadFunction($name);
> break;
> case SPL_AUTOLOAD_CONSTANT: return $this->loadConstant($name);
> break;
> default: return false; break;
> }
> }
> // Here load*() functions
> }
>
> spl_autoloader_register(new MyAutoloader, SPL_AUTOLOAD_ALL);
>
>
> Just my thoughts
>
>
> >
> > François
> >
> >
>
>
> --
> github.com/KingCrunch