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.]

Mastodon