Home arrow PHP arrow Page 4 - Deferring Class Property Creation with Lazy Loading

Building a final script - PHP

Welcome to the conclusion of a five part series that shows you how to implement lazy and eager loading in PHP 5. These two design patterns allow you to handle the resources for an application in very different ways. Through numerous code examples, I've demonstrated when it is appropriate to use each method.

TABLE OF CONTENTS:
  1. Deferring Class Property Creation with Lazy Loading
  2. Review: eagerly loading properties of a class
  3. Applying lazy loading to class properties
  4. Building a final script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 24, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I promised in the section that you just read, below I coded a sample script that shows how to add on request some properties to an instance of the "User" class defined previously. Here's the script: 

// create instance of 'User' class

$user = new User();

// create some properties

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';

$user->email = 'alejandro@domain.com';

// display user data

echo $user;

As you can see from the above example, once an instance of the "User" class has been properly spawned, it's really easy to add to it some properties on the fly, which shows that applying the lazy loading pattern to the properties in question is indeed very simple.

And finally, with this last example I'm finishing this series of articles about implementing eager and lazy loading in PHP-driven environments. Of course, I'm only scratching the surface when it comes to using these patterns in real-world cases, but hopefully the set of examples that you learned in these tutorials will give you a good idea of how to utilize them in some other useful ways.

Final thoughts

It's hard to believe, but we've come to the end of this series. Hopefully this journey has been educational, since in it you learned how to implement the lazy and eager loading patterns in different cases using PHP 5.

As you saw through the code samples shown here, when used in a clever way these patterns can be really powerful for boosting the performance of web applications, particularly of those that work with databases and rely on an object-oriented approach to perform different tasks.

See you in the next PHP development tutorial!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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