Home arrow PHP arrow Page 3 - Including Files Recursively with Loader Applications in PHP

Recursively including PHP files - PHP

Welcome to the fourth chapter of the series that shows you how to build loader applications with PHP. Made up of seven parts, this series uses a variety of code samples to teach you how to create modular programs. These programs are capable of recursively including files required by a given application, without having to explicitly call any “include()/include_once()” or “require()/require_once()” PHP function.

  1. Including Files Recursively with Loader Applications in PHP
  2. Review: the loader() class so far
  3. Recursively including PHP files
  4. Changing the location of required files
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 11, 2009

print this article



As I explained in the segment that you just read, the best way to understand how the previous “Loader()” class functions is by developing some concrete examples that show how to use it. So, since the class’s main task is to include required files dynamically, I’m going to use the two sample files created in preceding articles. They looked like this:





echo ' This file has been loaded with the Loader class.' . '<br />';







echo 'This file has been loaded at the following time: ' . date('H:i:s');



Now that there are two files available to be included by the loader class, it’s time to create a hypothetical scenario. Say the files in question are located on a directory called “folder1” on the web server (yes, my creativity really blows me away sometimes, especially when it comes to naming directories in a fancy way), which is one level below the location at which the loader class resides. 

Based on this simple directory hierarchy, it’s perfectly possible to code an example where the loader class includes the two previous files after performing a recursive search. The corresponding code sample would look as follows:


// create instance of Loader class

$loader = new Loader();

// set file to load


// set path of specified file

$loader->set_path($_SERVER['DOCUMENT_ROOT'] . '/folder1);

// try to load specified file

$loader->load($loader->get_file(), $loader->get_path());

// set another file to load


// try to load specified file

$loader->load($loader->get_file(), $loader->get_path());


/* displays the following

This file has been loaded with the Loader class.

This file has been loaded at the following time 20:33:25



From the example shown above, it’s clear to see how the loader class really works. In this specific case, the starting path we use to look for a required file is simply the aforementioned “folder1” directory, which is located one level below the web server’s root -- but naturally the path can be easily modified and make the class seek for other locations. 

Once the appropriate path has been set, the pertinent “load()” method is called dynamically to include  the “sample_file1.php” file, and then the remaining one, called “sample_file2.php.” This demonstrates the actual functionality of the “Loader” class when it comes to including specified files using a recursive search. 

While the previous example should be illustrative enough to show how the loader class does its thing, I’d like to finish this tutorial by creating a final code sample that demonstrates the capability of the class when the targeted files to be included are located two levels below the web server’s root. 

This wrapping example will be developed in the last segment of the article. Please click on the link that appears below and read the next few lines. We’re almost finished!

>>> 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: