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

Lazy loading in action - 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



In reality, building a script that lazy-loads the previous "User" class is a straightforward process that can be accomplished in a few simple steps, thanks to the brand new functionality that I gave the pertinent loader module.

To demonstrate the veracity of my claim, below I coded that script for you, so you can quickly grasp how it works. Here it is:

// create instance of 'User' class only when the script requires it

try {

// create instance of 'User' class

$user = new User();

echo $user;


catch (FileNotFoundException $e){

echo $e->getMessage();




catch (ClassNotFoundException $e){

echo $e->getMessage();



Do you realize how simple it is to switch from a script that eagerly loads a sample class to another one that lazy-loads it? And the best part is that this change was accomplished by means of a small modification introduced in the loader module. What else can one ask for?

Feel free to tweak all of the code samples that you saw in this tutorial. Doing so will give you a better understanding of how to apply the lazy loading pattern in PHP 5. The experience will be pretty educational, believe me.

Final thoughts

In this third episode of the series, I explained how to improve the performance of a script in PHP 5 by using the lazy loader pattern. As you saw for yourself, the proper usage of the pattern allowed us to include a class only when it was requested, thus avoiding unnecessary overheads during the script's execution.

Logically, it's possible to utilize the pattern when instantiating classes, a procedure not surprisingly known as lazy instantiation. Nonetheless, this topic is outside of the scope of this series of articles, so it's time to focus on the concepts that will be deployed in the upcoming part.

In that tutorial, I'm going to discuss how to use eager loading when working with the properties of a class. So, here's my little piece of advice: don't miss the next article!

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