At this point, I'm sure you want to learn how to build a file loading class with recursive searching capabilities. However, before we do that, it would be helpful to quickly recall the definition and usage of the “Loader” class developed in the preceding part of the series, so you can compare it with the one that I plan to create. Below I listed the full source code of the previous loader class, accompanied by a short example that shows how to work with it. First, here’s the signature of the “Loader” class:
// define the loader class (the 'load()' method is declared static) class Loader { // constructor (not implemented) public function __construct(){}
// load specified file public static function load($filepath) { if (!file_exists($filepath)) { throw new Exception('The specified file cannot be found!'); } require_once($filepath); } }
Next, we'll reintroduce the two sample files that will be included by the class. Here they are:
('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 finally, here’s a small code snippet that shows the “Loader” class in action, using its static “load()” method:
<?php try { // call 'load()' method statically and load specified files Loader::load('sample_file1.php'); Loader::load('sample_file2.php');
/* displays the following This file has been loaded with the Loader class. This file has been loaded at the following time 11:37:09 */ } catch (Exception $e) { echo $e->getMessage(); exit(); } ?>
From the code sample shown above, it’s clear to see how simple the logic is that is implemented by the pertinent “Loader()” class. All it does is include a specified file via its static “load()” method, which is merely a proxy for the native “require_once()” PHP function. In general terms, due to its basic structure, this class could be considered a file loader helper, rather than a core library of a fictional framework. Of course, as you might have imagined, it’s perfectly feasible to use the skeleton of the class and construct a more advanced and complex version of it, capable of searching recursively targeted files and including them inside a PHP application. That’s exactly what I’m going to do in the course of the following section: build a recursive file loader class. So, if you want to learn the full details of this process, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|