Home arrow PHP arrow Page 3 - An Object-Oriented Approach to Lazy and Eager Loading in PHP 5

Building a basic loader class - PHP

Welcome to the second installment of a series that shows you how to implement lazy and eager loading in PHP 5. Through a strong hands-on approach, this series teaches you how to use these patterns in some typical scenarios. In this way, you'll grasp their underlying logic and learn quickly how to take advantage of their functionality to speed up your own PHP-based programs.

  1. An Object-Oriented Approach to Lazy and Eager Loading in PHP 5
  2. Review: eager loading in PHP 5
  3. Building a basic loader class
  4. Using the Loader class in a sample script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
September 16, 2009

print this article



As I said in the section that you just read, I'd like to provide you with an example that shows how to eagerly include the previous "User" class by using a stricter object-oriented approach. To accomplish this, I'll code a simple loader class whose main goal will be statically including a specified file into a given script.

The complete definition of this brand new loader mechanism, along with a couple of custom exception classes, is shown below. Look at them, please:

// create custom FileNotFoundException exception class

class FileNotFoundException extends Exception {}


// create custom ClassNotFoundException exception class

class ClassNotFoundException extends Exception {}


// define 'Loader' class

class Loader {


public static function load($class)


if (class_exists($class, FALSE))




$file = $class . '.php';



throw new FileNotFoundException('File ' . $file . ' not found.');


require $file;


if (!class_exists($class, FALSE))


eval('class ' . $class . '{}');

throw new ClassNotFoundException('Class ' . $class . ' not found.');




As depicted above, the "Loader" class is comprised of a single static method called "load()," which implements the logic required to include the definition of a specified class into a calling script. Also, I added a couple of exception subclasses to handle more specifically any errors that might arise when performing the inclusion process.

So far, so good. At this point, I'm pretty sure that you already grasped the underlying logic of the above "Loader" class, right? Then it's time to build a script that uses this loader module to eagerly include the sample "User" class.

Want to see how this will be done? Click on the link that appears below and read the next section.

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