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.

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

TOOLS YOU CAN USE

advertisement

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
   

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 11 - Follow our Sitemap

Dev Shed Tutorial Topics: