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.

Mastodon