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!
blog comments powered by Disqus |
|
|
|
|
|
|
|