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

Review: a basic file loading system with PHP 5 - 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

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.



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