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

A static file loading 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.

  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



As I anticipated in the segment that you just read, one of the simplest ways to use the “Loader” class without having to create an instance of it consists of calling its “load()” method out of the object context, or in other words, statically.

PHP does support calling a non-static method statically (which is not supported when using static class properties), but to assure that the “load()” method will always be invoked statically, I’m going to refactor the class, this time declaring the method obviously static.

Does all of this jargon sound a bit confusing to you? Fear not. Only pay attention to the enhanced version of the “Loader” class, after implementing this subtle change. Here it is:

// define the loader class (the 'load()' method is declared static)

class Loader


// constructor (not implemented)

 public function __construct(){}


// load specified file

public static function load($filepath)


if (!file_exists($filepath))


throw new Exception('The specified file cannot be found!');





That was easy to code and read, wasn’t it? As you can see, now the “Loader” class looks nearly identical to its previous version, except for one small but crucial modification: its “load()” method has been declared static explicitly via the “static” keyword. This means that if the method is eventually called in the object scope (read dynamically), the PHP engine will complain loudly about this and throw an error.

Naturally, the most evident benefit in declaring this method static is that there’s no need to spawn an instance of the “Loader” class to call it. In this case, I’m getting the best of both worlds: I’m still able to include files using the “load()” method, but no object needs to be created.

However, the best way to understand the nitty-gritty of this process is by coding a concrete example. So, the last section of this tutorial will be dedicated to develop that example, so you can see how the improved version of the “Loader” class is really much more flexible and efficient.

Thus, don’t waste more time; read the following segment. It’s only one click away.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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