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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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();

exit();

}

 

catch (ClassNotFoundException $e){

echo $e->getMessage();

exit();

}

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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: