There is an RFC already that discusses the topic at length, we should really have this functionality:
https://wiki.php.net/rfc/autofunc On Wed, Nov 7, 2012 at 9:12 AM, Sebastian Krebs <krebs....@gmail.com> wrote: > 2012/11/7 Laupretre François <francois.laupre...@francetv.fr> > > > > De : sebastian.krebs.ber...@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 >