by Alan Langford | 2008-01-08 | It's a Code, Code World
I just added a class to the AP5L PHP library that does something I’ve wanted for a long time.
Most applications have a debug mode that shows developers what’s going on under the hood, so that they can diagnose problems. The problem with these modes is always selectively controlling what gets displayed at runtime. Most of the time, debugging is an on/off switch. Turn it on and you get reams of information. Usually only a small part is relevant and you have to dig through it all to find the information you need.
The AP5L_Debug class maintains an array of debug settings. The format of a setting is “type@namespace::class::method”. Any value can be associated with a setting. The setting mechanism allows simple fine-grained control over what gets written as diagnostics. Want to turn on all the diagnostics in MyClass? Simple. Do it with AP5L_Debug::getInstance() -> setState(‘::MyClass’, true); Only interested in the close function? AP5L_Debug::getInstance() -> setState(‘::MyClass::close’, true);
The real joy of this is that the actual output statements remain very simple. Just use AP5L_Debug::getInstance() -> write(‘My debugging message); Debug automatically figures out the class and method names, and looks for relevant settings. Namespaces will be implemented when they arrive in PHP 5.3.
There’s some more great features as well. Check out the comments at the beginning of the AP5L_Debug class.
by Alan Langford | 2008-01-08 | It's a Code, Code World
While working on a unit test facility for the “Joomla!” project, one of the developers made me aware of functions in the “Standard PHP Library” that let you do some powerful things with class auto-loading.
One of the biggest challenges with code libraries is making them easy to configure in a wide range of server environments. PEAR is a large, powerful set of packages that is useful in just about any application, but getting the installation right can be a real pain, particularly in shared hosting environments.
The SPL allows an application to manipulate a stack of auto-load functions. So now to use AP5L, all you have to do is include the base AP5L.php file and then call AP5L::install(). The “installer” injects an auto-loader that recognizes all AP5L classes. Since the code for these classes is relative to the base AP5L.php, it can derive the appropriate file name from the class name with ease. If the class isn’t part of AP5L, it simply passes it along to the next loader in the stack.
AP5L::install() also has an option for a more traditional approach, adding the install path to PHP’s include file path. However this leaves the application with the responsibility of including the class files before they’re used. In highly tuned systems with performance optimizers there are some good reasons to load all classes in one shot, and this option makes that slightly easier. [If you’re really interested in performance, the best easy thing to do is to merge everything into one big file, stripped of comments and whitespace, but that’s another topic.]
by Alan Langford | 2008-01-07 | It's a Code, Code World
One day I needed a simple form for list subscription that needed a CAPTCHA image and I didn’t want to go through the trouble of trying to crowbar it into any of the “complex” PEAR form packages.
So I thought “why not build something really simple and lightweight that will create and validate a simple form?” I wanted three methods: create, validate, and save. Sweet, simple, easy to code.
That might not have been the smartest thing I ever did. (more…)
by Alan Langford | 2008-01-03 | It's a Code, Code World, Observations on Everything
This one’s short: A slogan I came up with for the open source movement “We’re here to dominate the World, because domination should be free and democratic.”
Recent Comments