Definitely, a good epilogue for this first chapter of the series will be demonstrating how the file loading class that you saw in the previous section can be put to work. With that premise in mind, below I included a short code sample that shows how to include the two sample files created earlier by using the loaded class. Here it is:
// create instance of loader class
$loader = new Loader();
// load specified files
/* displays the following
This file has been loaded with the Loader class.
This file has been loaded at the following time: 20:25:36
catch (Exception $e)
Well, as you can see in the above example, the two basic files defined previously have been successfully included by the “Loader” class via its pertinent “load()” method. In this particular case, it’s been assumed that both the class and the files being loaded reside under the same directory on the web server, but naturally it’s possible to specify the full paths of these files, if they’re incidentally located on a different folder.
And with this last example, I’m finishing this introduction to building loader applications with PHP 5. This first part is pretty simple to digest, at least for experienced developers, but this is only the beginning.
In the interim, feel free to edit all of the code samples included in this tutorial, and try to create your own file loader class. The experience will be truly educational.
Over this first episode of the series, I explained how to construct a couple of basic file loading systems. The first mechanism used a procedural approach, while the last one relied on a class to load a specified file.
However, I’d like to point out one important thing with reference to the way that the loader class was built. As you’ll recall, its “load()” method was called in an object context (in other words, dynamically), in order to include a specific file. This means that an instance of this class needed to be created -- something which can be easily avoided.
But how can we do this? Well, by simply calling the mentioned method statically, it’s possible to avoid the unnecessary instantiation of the loader class. But the full details of this process will be discussed in the next article, so now that you’ve been told, you simply can’t miss it!
blog comments powered by Disqus