Home arrow PHP arrow Page 4 - Working Out of the Object Context to Build Loader Apps in PHP

The Loader class in action - PHP

Welcome to the fifth part of an eight-part article series that teaches you how to build loader applications with PHP. In this installment of the series, I explain how to create a small, efficient file loader class, with a difference: no instance of it needs to be spawned to include a targeted file, thanks to the implementation of a static recursive loading method.

TABLE OF CONTENTS:
  1. Working Out of the Object Context to Build Loader Apps in PHP
  2. Review: recursive file loading program
  3. Improving the definition of the Loader class
  4. The Loader class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 25, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

Certainly, the best way to demonstrate how the enhanced version of the "Loader" class works is through a concrete example. Therefore, bearing that concept in mind, below I included an easy-to-grasp code snippet, which uses the static "load()" method of the class to include the two sample PHP files that you saw at the beginning of the article. 

Here's the example:

 

// example of usage of the recursive loader class

// prepare path to search for the specified file

$path = $_SERVER['DOCUMENT_ROOT'] . '/path';

// try to load the first specified file

Loader::load('sample_file1.php', $path);

// try to load the second specified file

Loader::load('sample_file2.php', $path);

 

/* displays the following

This file has been loaded with the Loader class.

This file has been loaded at the following time 22:18:21

*/

 

That was pretty simple to code and read, wasn't it? As you can see, now the sample files have been successfully included by the above script, without having to spawn an unnecessary instance of the "Loader()" class. 

In this particular case, I decided to feed the method with a fictional starting path to look for those files, but as homework for you, I suggest you try using different paths and changing their locations to see how the class behaves in each case. The experience will be pretty instructive, and also fun. 

Final thoughts 

That's all for the moment. In this fifth installment of the series I explained how to create a small -- yet efficient -- file loader class, but in this particular case, no instances of it need to be spawned to include a targeted file, thanks to the implementation of a static recursive loading method. 

Also, as you may have noticed, the loader class developed so far doesn't use the "__autoload()" magic function included with PHP 5. So, it's time to take advantage of it, don't you think? Therefore, in the upcoming article I'm going to discuss how to build a file loading program  using this handy function. 

As usual, here's my final suggestion: don't miss the next part!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: