Home arrow PHP arrow Page 3 - Introducing the Lazy Loading Pattern

Introducing the lazy loading pattern - PHP

Welcome to the third part of a five-part series on lazy and eager loading in PHP 5. In this part of the series, you'll learn about the lazy design pattern, and how to use it effectively in your applications.

  1. Introducing the Lazy Loading Pattern
  2. Review: object-oriented lazy loading
  3. Introducing the lazy loading pattern
  4. Lazy loading in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 17, 2009

print this article



To apply the lazy loading pattern within the script that you learned before, it's necessary to introduce a subtle change into the corresponding loader module. Basically, this modification will consist of registering the "load()" method of the "Loader" class with the "spl_autoload_register()" PHP function, so the method in question can be called automatically when a script attempts to include a class on demand.

Having explained that, here's the enhanced definition of the loader module:

// 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.');




That was really simple to code and read, wasn't it? At this point, the loader module has suddenly turned into a true autoloader mechanism that will include a specified class on request, thanks to the use of the aforementioned "spl_autoload_register()" PHP function.

Believe it or not, this small modification into the source code of the module permits to easily build scripts, or even entire applications, that include classes only when they are really required -- or in other words, that implement the lazy loading pattern.

But if you're like me, you now want to see how the revamped version of the previous loader module can be put to work with the "User" class. Am I right? Great! In the last section of this article I'm going to create a basic script that will load that class in a lazy manner.

To see how this script will be developed, go ahead 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: