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:
('sample_file1.php')
<?php echo ' This file has been loaded with the Loader class.' . '<br />'; ?>
('sample_file2.php')
<?php 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 $loader->set_file('sample_file1.php'); // 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 $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 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|