PHP Library Tips
Kellan Elliott-McCrea (author of the popular Magpie RSS parser): A Few Tips for Writing Useful Libraries in PHP. Kellan makes the interesting observation that PHP encourages a culture in which most development occurs in the context of either full applications or C extensions, with few people devoting themselves to releasing libraries.
Keith - 13th August 2003 18:15 - #
Simon Willison - 13th August 2003 18:16 - #
Maybe if PHP coders didn't have such a poor example for how to write PHP libraries we would have much better code to reuse as well as reference for how to create good libraries. That said, some people are cleaning up PEAR and contributing a lot of good stuff.
Cheers,
David A. Ulevitch - 13th August 2003 21:12 - #
Nice article. One thing you have to take into account though is PHP re-parses / executes code at run time. What shocked me recently (while playing with XDebug) is the performance cost of using require_once(), for example. Now there's premature optimization and all that but "going Java" with PHP classes can be a massive overhead. Where PEAR is concerned, there is a valid point to "on loading your package is requires another 20 class files but I only needed one".
One way to cut out PHP overhead is to "compile" your HTML, using some kind of observer mechanism which flushes it one updates. Technically I guess this is optimization - the motivation is only for performance. But implementing it has to be part of the core of a PHP app, so it's a design consideration from the start. Which reminds me, if you'll excuse the annoucement - got a Simple "HTML Compiler" up here http://simplet.sourceforge.net/
Harry Fuecks - 14th August 2003 00:17 - #
This optimisation makes a lot more sense than you think. Not only is apache very good at serving static files quickly, you also get the benefit of its better HTTP compliance. Can you be bothered to implement If-Not-Modified in your PHP? No? Then let Apache do it. And it will implement content negotation, byte ranges and a whole host of other http stuff that you shouldn't have to be bothered by. Also, you can sprinkle some mod_expires bits in there as well, if you want to be really cache friendly.
-Dom
Dominic Mitchell - 14th August 2003 08:15 - #
Hopefully someone here can reember where to find this tutorial I've forgotten :-)
It talked about writing PHP classes and how there were two ways to write methods - one was to pass loads of parameters (either in an array or separately), and the other was to use other methods to set the parameters first. But I've lost the URL - does anyone else remember it?
Peter - 14th August 2003 09:23 - #
Blinks. Blinks again.
How on earth did I miss xml_set_object ? I've written literally dozens of XML parsing classes, and every time except this one I have used xml_set_object. I guess it's I've never bothered memorising the rather convoluted set of calls you have to make to set up a SAX parser - instead I copy the basic code from somewhere every time and build on that. Somehow I managed to copy it from somewhere that wasn't using the xml_set_object call.
I still wouldn't have had the problem if PHP 4 had sane references though.
Simon Willison - 14th August 2003 10:33 - #
me - 17th August 2003 19:02 - #
me - 17th August 2003 19:02 - #