Home arrow PHP arrow Page 2 - Lazy and Eager Loading in PHP 5

Eager loading in PHP 5: an example class - PHP

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.

TABLE OF CONTENTS:
  1. Lazy and Eager Loading in PHP 5
  2. Eager loading in PHP 5: an example class
  3. Completing the User class
  4. Applying the eager loading design pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
September 10, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I mentioned in the introduction, lazy and eager loading are complementary patterns that have their own pros and cons, logically depending on the context where theyíre applied. However, in this first part of the series Iím going to focus my attention specifically on the latter technique, so you can quickly grasp its rationale and decide where and when to use it.

Having clarified that point, itís time to demonstrate a basic usage of eager loading. Iím going to build a simple class. It will do nothing special except for storing data on some fictional users in the form of properties. Please, pay attention to the initial definition of this class, which is as follows:

// define 'User' class

class User {

 

private $fname = 'Alejandro';

private $lname = 'Gervasio';

private $email = 'alejandro@mydomain.com';

 

public function __construct($fname = '', $lname = '', $email = '')

{

if (is_string($fname) and !empty($fname))

{

$this->fname = $fname;

}

if (is_string($lname) and !empty($lname))

{

$this->lname = $lname;

}

if (is_string($email) and !empty($email))

{

$this->email = $email;

}

}

}  

Certainly, it's very easy to understand the way that the brand new ďUserĒ class functions. As I explained previously, it actually does nothing but save basic data on a particular user to their declared properties. Period.

For this example, the assignment of new values to those properties is performed exclusively through the classís constructor, but logically itís possible to create some setter methods that do the same thing.

So far, so good, right? At this stage, the ďUserĒ class in its current state is admittedly pretty useless, for the reason given earlier. If the class is only capable of storing user-related data, then it would be desirable to code a few additional getter methods that permit it to retrieve the data from the outside world.

But, wait a minute! You might be wondering what this class has to do with implementing the eager loading pattern, after all. Well, bear with me for the moment and read the following section, where Iím going to add to the class the aforementioned getter methods. Then, once the development of the class has been completed, youíll see how it fits into the schema imposed by the pattern.

Now, click on the link that appears below and keep reading.



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