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