HomePHP Using Directory Iterators to Build Loader Apps in PHP
Using Directory Iterators to Build Loader Apps in PHP
Welcome to the final part of an eight-part series on building loader applications in PHP. In this part, we'll improve on the loader class we developed in the previous part by using certain specific functions from the Standard PHP Library (SPL).
Among the bunch of useful features that were incorporated into PHP 5 a long time ago, aside from the improved object model, there's also a set of functions and classes that comprise what's commonly called the Standard PHP Library (SPL).
As you may have heard, this library permits you to perform several clever tasks through a powerful API, such as working with predefined interfaces, traversing arrays and directories easily, loading classes automatically, and so forth. But it's quite possible that at this point you may be wondering what the SPL has to do with building loader applications.
Well, I'm glad you asked, since some of the classes and functions that come packaged with the Standard PHP Library allow you to develop this kind of application in a much more compact and readable way, simplifying the code required for including specified resources and making it easier to create recursive methods.
In the preceding part of this series I explained how to create a loader program that could recursively search for a specified class through the file system on the web server.
While this sample application does its work pretty well when it comes to including classes in a transparent way, there's plenty of room to improve its recursive loading method. And here's where the SPL comes in, since it's possible to use a combination of its "spl_autoload_register()" function and its RecursiveDirectoryIterator class to refactor the method in question and make it shorter and tightrer.
So, with that goal in mind, in this final chapter of the series I'm going to improve the loader class developed in the previous one by incorporating some of the aforementioned SPL functions and classes.
Are you ready to discover the power of using directory iterators for building a more efficient class loading program in PHP 5? Then, don't waste more time in preliminaries; start reading right now!