In case you still haven’t read the previous chapter of this series, where I went through the development of a pretty basic file loader class in PHP 5, I've included its full signature below, accompanied by an example of its usage, so you can understand how it works. Please take a close look at the structure of the sample “Loader” class: class Loader { // constructor (not implemented) public function __construct(){}
// load specified file public function load($filepath) { if (!file_exists($filepath) OR is_dir($filepath)) { throw new Exception('The specified file cannot be found!'); } require_once($filepath); } } Indeed, the definition of the above “Loader()” class is so simple to understand that it doesn’t bear any further explanation. Therefore, let me show you another example aimed at illustrating how to use the class, this time for including a pair of PHP files that were created in the preceding article. First, here are the aforementioned sample files, called “sample_file1.php” and “sample_file2.php” respectively: ('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 you’ve seen how the two trivial files listed above look, it’s time to code an example that shows how to use the previous “Loader” class to dynamically include these files. Here you have it: <?php
try { // create instance of loader class $loader = new Loader(); // 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: 2:27:15 */ } catch (Exception $e) { echo $e->getMessage(); exit(); } ?> Definitely, from the code sample shown above, it’s extremely easy to grasp the logic that drives the “Loader()” class. Its core “load()” method behaves like an improved proxy for the “require_once()” PHP native function, plus the incorporation of a basic error handling mechanism, in this case implemented via the Exception class bundled with PHP 5. So far, nothing spectacular is happening here, right? While the “Loader” class does decent work because it permits us to include several files by dynamically calling its “load()” method, it has a serious drawback. As I mentioned a moment ago, the “load()” method is invoked in the object context, which means that an instance of the “Loader” class has been previously created. Is this really necessary? Not at all, believe me! And when I say this, I am only attempting to correctly apply the foundations of object-oriented programming. Since PHP permits us to statically call methods of a class, it’s feasible in this particular case to declare the “load()” method static and include the specified files without having to deal with unnecessary class instances. Since it will be a considerable enhancement of the “Loader” class, in the next section I’m going to switch the default declaration of its “load()” to static, to avoid the issue discussed earlier. To learn more about how this will be done, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|