Page 1 of 1
__autoload() .. to use or not to use.
Posted: Mon Apr 21, 2008 2:50 am
by onion2k
Sort of related to the "
Requirements for things you will release" thread, this weekend I've been trying to work out whether or not to use __autoload() in a new project. I really like autoloading, it's useful and it can save on resources by not loading unnecessary classes. Ace. On the face of it I'd use it in a shot.
But..
The application I'm writing is aimed at being "embeddable" into other websites. The problem with that is that if I've autoloaded things then, if the website also uses an autoloader the chances are they won't play very nicely together. There is a possible solution I could implement that would use __autoload() if the app was running in "standalone" mode or include all the classes using a require_once() include file if it's embedded, but I don't really like that idea.
Have you ever faced this dilemma? What would you do?
Re: __autoload() .. to use or not to use.
Posted: Mon Apr 21, 2008 2:56 am
by s.dot
Hmm, how savvy will the intended audience be? Me thinks if they do use autoload and they can provide the scenario it is used in, is_callable() might be useful.
Re: __autoload() .. to use or not to use.
Posted: Mon Apr 21, 2008 3:05 am
by Chris Corbyn
spl_autoload_register().
You end up with min requirements being PHP 5.1.2 for that though.
Re: __autoload() .. to use or not to use.
Posted: Mon Apr 21, 2008 3:20 am
by onion2k
ooo.. that looks useful. Cheers.
Re: __autoload() .. to use or not to use.
Posted: Tue Apr 22, 2008 1:45 pm
by Ambush Commander
Using autoload in HTML Purifier was one of the primary migration concerns when I released 3.1.0. To get an idea of how much documentation I had to write, check
this page out. And I'm not even sure if that's enough.
But I do think autoload is good. It gives you lots of flexibility with the source files; with a little code generation I can run HTML Purifier without autoload on at all.
Re: __autoload() .. to use or not to use.
Posted: Wed Apr 23, 2008 10:59 am
by Maugrim_The_Reaper
Look on the bright side. Autoloading lost most of it's glaring problems and issues after PHP 5.2 - it's now adopted (in some incarnation) by PEAR and the Zend Framework. The ZF is kind of questionable - they still use require_once() everywhere which defeats part of one advantage (lazy loading).
I think the main challenge is making the introduction of autoloading via SPL as simplistic as possible, and ensuring it's optional! I still tend to implement an application specific __autoload simply because most PHP5 libraries utilise the ultra predictable PEAR naming convention. Why have 5 autoloading functions, when 1 generic one works for everything?
Re: __autoload() .. to use or not to use.
Posted: Mon Apr 28, 2008 4:00 pm
by RobertGonzalez
+1 spl_autoload_register().
Re: __autoload() .. to use or not to use.
Posted: Mon Apr 28, 2008 4:23 pm
by Ambush Commander
I just thought of this...
Another consideration when you're using spl_autoload_register is the order and specificity of the autoloaders. It's not unusual for the base application to specify a catch-all autoloader. However, if a library autoloader is registered later (it probably will be), it will never be seen: the catch all will always process it. This may or may not cause errors.