As I explained in the segment that you just read, it would be really useful to declare the "load()" method of the previous file loading class static, to make it callable only out of the object's context. To do so, I'm simply going to refine the signature of the class in question, which now will look as follows:
class Loader { // constructor (not implemented) public function __construct(){}
// load recursively a specified file public static function load($file, $path) { $filepath = $path . '/' . $file; if (file_exists($filepath)) { require_once($filepath); } else { if (!is_dir($path)) { throw new Exception('The supplied path is invalid!.'); } if (FALSE !== ($handle = opendir($path))) { // search recursively the specified file while (FAlSE !== ($dir = readdir($handle))) { if (strpos($dir, '.') === FALSE) { $path .= '/' . $dir; self::load($file, $path); } } closedir($handle); } } throw new Exception('The specified file was not found!.'); } }
As you can see, not only have setters and getters been removed from the "Loader" class, but as I mentioned before, its "load()" method has been declared static for the reasons given previously. Besides, it's valid to notice another subtle enhancement that has been also introduced in this method: it now will throw a regular exception if the file to be included isn't found across the supplied path. Definitely, the loader class is now a bit more functional and, best of all, it can be used for including files dynamically without having to deal with an instance of it. So, what's the next step to take? Well, if you're like me, then you'll surely want to see it in action, right? Therefore, in the final section of this article I'm going to develop another example, which will be aimed at demonstrating how to use the loader class out of the object scope. To learn the full details of how this concluding example will be created, click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|