Home arrow PHP arrow Page 4 - Lazy and Eager Loading with Object Properties

Applying eager loading to an instance of the User class - PHP

Welcome to the fourth chapter of this series on implementing lazy and eager loading in PHP 5. In five friendly tutorials, this series walks you through the basics of using these powerful approaches for including classes on request in your scripts, and teaches you how to work with these patterns when manipulating properties of a certain class.

  1. Lazy and Eager Loading with Object Properties
  2. Review: using the lazy loader pattern with PHP 5
  3. Simple eager loading: examining the user class
  4. Applying eager loading to an instance of the User class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
September 23, 2009

print this article



The best way to understand how the eager loading pattern is used by the sample “User” class is simply by coding a script that creates an instance of it without passing any incoming parameters to its constructor.

The code sample that does that is the following, provided that the class in question has been saved to a file called “user.php”:

require_once 'user.php'

// create instance of 'User' class

$user = new User();

// display user data

echo $user;

If you run this script on your own web server, the you’ll see that it outputs to screen the default values assigned to the “User” class properties, once an instance of it is created. This naturally happens due to the call to the magic “__toString()” method, but you’ll get a non-empty result when invoking any of its getter methods as well.

While extremely simplistic, this example demonstrates that it’s possible to implement eager loading at lower levels and exploit its eventual benefits, even when in some cases these may be negligible.

Feel free to edit all of the examples developed in this tutorial. Doing this surely will arm you with a better knowledge of eager loading in PHP 5.

Final thoughts  

In this fourth installment of the series, I demonstrated through a simple example that it’s feasible to implement the eager loading pattern with the properties of a basic class. Nonetheless, the experience would be incomplete if I don’t show you how to apply lazy loading to the properties of the same class.

That will be exactly the subject that I plan to discuss in the final part of this series, so I recommend that you not miss it!

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