Home arrow PHP arrow Page 4 - Using Static Methods to Build Loader Apps in PHP

Loading files using a static class method - PHP

If you’re a PHP developer who has already worked with popular third-party frameworks like Kohana and CodeIgniter, or even better, have created one all by yourself, then you're familiar with building file loading applications. The process can be tackled pretty easily, either by using a procedural approach or the object-oriented paradigm. In this eight-part series, we take a close look at building file-loading applications. This second part of the series will focus on static methods.

TABLE OF CONTENTS:
  1. Using Static Methods to Build Loader Apps in PHP
  2. Review: a basic file loading system with PHP 5
  3. A static file loading method
  4. Loading files using a static class method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 03, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

To finish this second part of the series, I'm going to develop a short example to demonstrate how to include the pair of sample files shown previously by means of the “Loader()” class. In this case, of course, the “load()” method will be called statically.

Now that I have explained that, please pay close attention to the example, which looks like this:

<?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 2:45:18

 */

}

catch (Exception $e)

{

echo $e->getMessage();

exit();

}

?>

As shown above, it’s perfectly possible to load as many files as required via the “Loader()” class, without needing to create an instance of it. Of course, this is a great improvement in the way that certain dependencies are included into a given PHP application, even when it’s necessary to make one static call per requested file.

But I’m only scratching the surface when it comes to building loading programs with PHP 5, so for the moment feel free to tweak all of the the code samples developed in this article and try to create a better loader class. The exercise will be really worthwhile.

Final thoughts

In this second part of the series, I explained (through various code samples) how to enhance the file loader class created in the first chapter simply by declaring its “load()” method static, and obviously by calling it statically as well, when including a specific file.

This is definitely a great breakthrough toward the development of an efficient file loading application, but there is still plenty of room for further enhancements. Therefore, in the upcoming article I’m going to extend the functionality of the loader class by providing it with the capacity for including files recursively.

Want to learn the full details of this process? Then don’t miss the next chapter!



 
 
>>> 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: