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

Changing the location of required 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
  

I’d like to end this fourth part of the series by showing you one last example that illustrates how the loader class can be capable of including the same sample files that you saw in the previous section -- but this time, these files are placed under a directory that’s located two levels below the web server’s root. 

To demonstrate the recursive search abilities of the class in a case like this, below I included another code sample, where the files to be included now reside in a fictional new folder called “folder2.” Study the following example closely to grasp how it works:

 

// create instance of Loader class

$loader = new Loader();

// set file to load

$loader->set_file('sample_file1.php');

// set path of specified file

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

// try to load specified file

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

// set another file to load

$loader->set_file('sample_file2.php');

// 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 22:21:09

*/

 

As you can see above, even when the sample files to be included now are located in a directory that’s two levels below the root of the web server, the loader class will load them successfully regardless of their location, thanks to the recursive method it employs to perform the inclusion process. 

Also, you may want to create your own examples with the loader class. Try putting your required files at different locations on the file system, and see how the class does a recursive search with the new supplied path. This exercise will be helpful for sharpening your skills in building loading programs with PHP 5. 

Final thoughts 

Over the course of this fourth article of the series, I proceeded to create some basic examples that hopefully demonstrated the actual functionality of the file loader class that you saw previously. 

As you saw in the examples, the class was capable of including a specific file or resource in a recursive way, as long as the file in question was located at the same level and below of the starting search path. This makes it a flexible and effective loading mechanism. 

In the article to come, I’m going to discuss how to take advantage of the functionality provided by the loader class, but without having to create an instance of it. 

Don’t miss the next part!



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: