In case you still haven't had opportunity to read the last article of the series, where I explained how to build a file loading class with recursive search capabilities, below I've included the class's full source code. Pay close attention to its definition, please:
<?php // define a recursive loader class class Loader { private $file = ''; private $path = ''; // constructor (not implemented) public function __construct(){}
// set file to load public function set_file($file) { $this->file = $file; }
// get file to load public function get_file() { return $this->file; }
// set path to load file public function set_path($path) { $this->path = $path; }
// get path to load file public function get_path() { return $this->path; } // load recursively specified file public function load($file, $path) { if (file_exists($file)) { require_once($file); return; } else { if (is_dir($path)) { if (FALSE !== ($handle = opendir($path))) { // search recursively the specified file while (FAlSE !== ($dir = readdir($handle))) { if (strpos($dir, '.') === FALSE) { $path .= '/' . $dir; $file = $path . '/' . $this->file; $this->load($file, $path); } } } closedir($handle); } } } }
If you look closely at the above "Loader" class, then you'll realize that apart from its setters and getters, its actual workhorse is its "load()" method. This method is not only is responsible for including a specified file via the "require_once()" PHP function, but performs this task by using a simple recursive algorithm that traverses the proper directories on the web server. So far, so good. Having quickly reviewed the definition of the file loader class, below I included a couple of examples that show how to include two basic PHP files. That being said, here are the two files to be included:
('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'); ?>
And here are two examples that demonstrate a concrete usage of the previous "Loader" class, using in each case a different path to look for the targeted files. Here's the first example:
// 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 10:32:51 */
Finally, here's the second example, which utilizes a distinct starting path to look for the specified files:
// 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 10:45:02 */
Well, I think that the above code samples should give you a clear idea of how the file loader class does its thing. However, as I mentioned in the introduction, it's necessary to spawn an instance of the class to include a targeted file, which in truth can be completely avoided simply by declaring its "load()" method static. So, based on this concept, in the section to come I'm going to improve the definition of the "Loader()" class by modifying the method discussed before, and incorporating into it a basic error handling mechanism as well. Now, click on the link below and proceed to read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|