Home arrow PHP arrow Page 4 - An Object-Oriented Approach to Lazy and Eager Loading in PHP 5

Using the Loader class in a sample script - PHP

Welcome to the second installment of a series that shows you how to implement lazy and eager loading in PHP 5. Through a strong hands-on approach, this series teaches you how to use these patterns in some typical scenarios. In this way, you'll grasp their underlying logic and learn quickly how to take advantage of their functionality to speed up your own PHP-based programs.

TABLE OF CONTENTS:
  1. An Object-Oriented Approach to Lazy and Eager Loading in PHP 5
  2. Review: eager loading in PHP 5
  3. Building a basic loader class
  4. Using the Loader class in a sample script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
September 16, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Having created a class that's capable of loading other classes via its static "load()" method, it's time to use it, right? To do so, below I coded another script that utilizes this loader module to eagerly include the previous "User" class.

Now, study the following code fragment:

try {

// load eagerly 'User' class

Loader::load('user');

/*

rest of logic goes here

*/

// create instance of 'User' class

$user = new User('Mary', 'Smith', 'mary@domain.com');

echo $user;

}

catch (FileNotFoundException $e){

echo $e->getMessage();

exit();

}

 

catch (ClassNotFoundException $e){

 echo $e->getMessage();

exit();

}

Here you have it. Through a few lines of strict object-based code it was feasible to successfully apply the eager loading pattern. As seen above, a couple of "catch()" blocks are used to intercept specific exceptions that might arise when including the file that contains the "User" class, but of course it's possible to remove this error handling mechanism if you don't want to deal with it.

Finally, I encourage you to edit all the code samples shown in this tutorial, so you can arm yourself with a better understanding of the eager loading pattern in PHP 5.

Final thoughts

In this second chapter of the series, I showed how to implement the eager loading pattern in PHP 5 by using a stricter object-based approach. The example that you saw a few lines ago applied this pattern to include a user-related class and create an instance of it without receiving an explicit request to do so, which show that the benefits of using this or any other pattern depend strongly on the development context.

In the upcoming article, I'm going to explain how to apply the lazy loading pattern with the same "User" class. Thus, if you wish to learn the full details of this process, then don't miss the next part!



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

Dev Shed Tutorial Topics: