HomePHP Using the _autoload() Magic Function to Build Loader Appps in PHP
Using the _autoload() Magic Function to Build Loader Appps in PHP
Welcome to the sixth part of an eight-part series that shows you how to build file loader applications in PHP. In this part I will discuss how to build a helpful file loading application by taking advantage of the nifty “__autoload()” magic function.
There are a number of tasks that PHP developers must tackle on a regular basis when building well-structured web applications. One of these is creating scripts that load a certain number of resources and dependencies required for those applications to work as expected.
While developing these file loading scripts can be as simple as using PHP includes for each requested file, there are specific situations when it’s necessary to create more complex loader programs that incorporate some additional features, such as automatic inclusion of dependencies, and recursive search of them across a given file system path as well.
Developing an application like the one described above with PHP 5 isn’t as hard as it may seem at first glance, especially when taking advantage of the functionality provided by the Standard PHP Library (SPL) in conjunction with the “__autoload()” magic function.
In the previous installments of this series I went through the process of creating a file loader class. This class was not only capable of seeking and including a targeted file by recursively traversing the directories and subdirectories of a supplied path, but it had the ability to perform this task without having to spawn any instances of the aforementioned class.
How was this achieved? Well, by simply calling the class's main loader method statically and nothing else. It was that easy, really.
But, as I mentioned a few lines before, it’s also feasible (and desirable) to construct file loading applications by combining certain built-in features of PHP 5, such as its “__autoload()” magic function, among others. Therefore, in this sixth chapter of the series I’m going to explain how to build a brand new file loader mechanism that will rely on this PHP 5 function.
Now, it’s time to leave all of the preliminaries behind us and start learning how to create a file loading system around the handy “__autoload()” function. Let’s begin right away!