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

Review: eager loading in PHP 5 - 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.

  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



The eager loading pattern can be used in cases ranging from the inclusion of dependencies to the instantiation of classes. Nevertheless, it'd be useful to recall the example created in the previous installment of the series, which demonstrated how to apply the pattern to loading a class that stored user-related data.

That being said, here's the definition of the 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;




// get user's first name

public function getFirstName()


return $this->fname;



// get user's last name

public function getLastName()


return $this->lname;



// get user's email

public function getEmail()


return $this->email;



// display user data

public function __toString()


return 'First Name: ' . $this->fname . '<br />Last Name: ' . $this->lname . '<br />Email: ' . $this->email;



As I explained before, this class does nothing particularly special. It can be used for easily saving information about some users, such as their first and last names, and their email addresses as well, to its private properties.

Obviously, building a class like this is only for example purposes, so let me go one step further and show you a script that includes the class using the eager loading pattern. Here it is:


require_once 'user.php'

/* rest of script code

goes here


// create instance of 'User' class

$user = new User();

// display user data

echo $user;


If you're wondering about the point of creating the above script, again I have to say that it's for demonstrative purposes. However, don't judge me harshly; pay close attention to the things happening behind the scenes within the script. As you can see, the previous "User" class is directly loaded via a regular PHP include, without taking into account whether or not an instance of it will be required by the program, something that only occurs at the end.

Though it may look rather primitive, this code sample shows a simple use of the eager loading pattern when including classes into a certain script. As you may have realized, in this specific case the pattern is detrimental to the script's performance, but like I said in the introduction of this series, there are cases when eager loading can be of great help, particularly when used for performing fewer SQL queries.

However, for the moment that topic is out of the scope of this article. Thus, now that you've hopefully understood how the previous example works, it's time to dig deeper into the usage of eager loading in PHP 5.

As you may have noticed, the script that eagerly loads the sample "User" class utilizes a mix of procedural and object-oriented code to implement the pattern. We can improve this by creating a class responsible for including that class via a static method.

Therefore, in the upcoming section I'm going to define a basic loader class that will avoid explicitly calling any PHP "require/require_once()" function when including a specified class.

To learn the full details of how this class will be constructed, go ahead and read the following segment. It's only one click away.

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