HomePHP Page 4 - Using Directory Iterators to Build Loader Apps in PHP
Putting the Autoloader class into action - 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).
As you may have noticed when I showed the improved signature of the "Autoloader" class, it used a constant called BASEPATH to start looking for a specified class. But in this particular case, this constant will be defined outside the class, instead of storing its value as a property.
Naturally, you can change this condition and take the approach that best fits your needs and requirements. So, having clarified that point, let me show you a simple script that demonstrates how to dynamically include the previous "User" class.
The code required to perform this task is as simple as this:
// create instance of User class (it's autoloaded by the Autoloader class)
$user = new User();
// display user data
In reality, apart from creating an instance of the sample "User" class and displaying some data on it, the above script only defines the corresponding BASEPATH constant and nothing else, since the inclusion of the class is handled behind scenes via the autoloading application.
Even though this concrete example only shows how to include a trivial class, it still illustrates in a nutshell how to build efficient loader programs in PHP 5 that take advantage of the functionality provided by the Standard PHP Library.
From this point onward, feel free to edit all of the code samples developed in this tutorial to help you get started developing your own file loading programs.
It's hard to believe, but we've come to the end of this series. I hope the experience has been educational, since through all the tutorials you learned how to build different kinds of file loader programs with PHP 5.
Ranging from explicitly using PHP includes to working with some of the functions and classes available in the Standard PHP Library, the variety of applications that load classes transparently is pretty huge. But, since the logic implemented by those programs is very similar, you shouldn't have major problems adding them to your toolbox.