Home arrow PHP arrow Page 4 - Building Loader Apps in PHP

The loader class in action - PHP

Loading sources on the fly is one of the most common tasks that PHP programmers have to tackle during the development of web applications. This typical situation must be faced independently of the scale of the programs being created. This means a loader mechanism must be developed. Keep reading as we take a close look at these mechanisms in this eight-part article series.

TABLE OF CONTENTS:
  1. Building Loader Apps in PHP
  2. Start building loading programs with a require_once() function
  3. Building a basic file loader class
  4. The loader class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
May 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

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:

<?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: 20:25:36

*/

}

catch (Exception $e)

{

echo $e->getMessage();

exit();

}

?>

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.

Final thoughts

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: