Home arrow PHP arrow 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).

  1. Using Directory Iterators to Build Loader Apps in PHP
  2. Review: the previous class loader program
  3. Improving the definition of the Autoloader class using directory iterators
  4. Putting the Autoloader class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
July 02, 2009

print this article



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:

// define base path to search for specified class

define('BASEPATH', $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/');

// create instance of User class (it's autoloaded by the Autoloader class)

$user = new User();

// display user data

echo $user; 

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. 

Final thoughts  

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.

See you in the next PHP development tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: