Before I start creating some examples aimed at illustrating how to use the file loading class built in the last article of this series, let's recall quickly how it was defined originally. So, as a reminder, here’s the full source that corresponds to that class. Pay close attention to it, 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); } } } }
Undeniably, aside from the non-implemented constructor, the workhorse of the above “Loader” class is clearly its “load()” method. It actually takes care of loading a targeted file via the “require()” PHP function. Of course, the most important detail to stress here is the method’s recursive implementation, which permits us to seek the required file through folders and subfolders within the specified path. Now that you’re hopefully familiar again with the way that the “Loader()” class functions, it’s time to begin coding some examples that will demonstrate its functionality in concrete situations. With that premise in mind, in the following section I’m going to set up a fictional scenario in which the loading class will be used for including a sample file, regardless of its location within a selected directory on the web server. To learn the full details of how this brand new example will be developed, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|