PHP
  Home arrow PHP arrow Lazy and Eager Loading in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Lazy and Eager Loading in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2009-09-10


    Table of Contents:
  • Lazy and Eager Loading in PHP 5
  • Eager loading in PHP 5: an example class
  • Completing the User class
  • Applying the eager loading design pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Lazy and Eager Loading in PHP 5
    ( Page 1 of 4 )

    As you may know, design patterns are proven, well-trusted solutions that tackle a specific problem that occurs frequently in software development. As with many other principles and paradigms in this area, some patterns are more popular and easier to learn than others, and when applied properly, can considerably improve the efficiency and performance of an application. This five-part article series will discuss two of these.

    In the case of PHP 5, which is better suited for developing web-based programs, there are two complementary design patterns that can be of great help in  building faster and more reliable applications. Of course, as this article’s title suggests, I’m talking about the lazy and eager loading patterns. Despite their rather funny names, they are pretty easy to implement in PHP 5-controlled environments.

    But before I start coding some functional examples in the next few lines, it would be useful to explain briefly what these patterns actually are, so you'll understand their underlying logic. First, the expression “lazy loading” refers to the ability of a program to load a specific resource only at the time it’s required, and not before.

    In this case, a resource can be anything that fits in into a program’s context, ranging from one or multiple classes, functions, database result sets, one or more objects, and so forth (feel free to add your own to the list). Naturally, lazy loading is a powerful pattern that very often is used in PHP 5 to speed up the performance of database-driven applications.

    For instance, it’s possible to “delay” or defer the instantiation of a certain class until a program really requests this process, thus preventing the overhead of storing an object in memory that will remain unused most of the time. In this  example, this procedure would be called “lazy initialization.”

    On the other hand, eager loading is the opposite of lazy loading. It simply refers to the process of loading or prefetching a resource with anticipation, regardless of whether or not it will be required by a program. At first glance, it seems to be a rather inefficient approach, but this is merely a misconception, trust me. Certainly, one common use of eager loading is when processing database result sets retrieved from two or more related tables. In a situation like this, it would be possible to prefetch all of the child records associated with a row in a parent table, in this way avoiding the need to perform additional queries.

    At this time, are you capable of spotting the differences between using lazy and eager loading? I hope so. But to clear up your doubts, in this group of articles I’m going to show you how to apply these powerful design patterns in some concrete situations using PHP 5. So start reading!



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

       

    PHP ARTICLES

    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...
    - Handling Attachments and Sending HTML Email ...
    - Sending Blind Carbon Copies with Swift Mailer
    - Using Swift Mailer's Cc MIME Header
    - Using Sendmail and Mail() with Swift Mailer
    - Using Different SMTP Transports with Swift M...


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek