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

Review: eagerly loading properties of a class - 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

In the introduction, I mentioned that it's possible to implement lazy loading with the properties of a class. However, before I start explaining how to do that, it'd be useful to briefly recall the example developed in the previous article. It demonstrated how to perform the inverse process, that is, apply the eager loading pattern to those properties.

Having explained that, here's the complete definition of the class that eagerly declares its properties. Take a look at it, please:

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;

}

}

In this specific case, the above "User" class does nothing especially useful or interesting, except for the fact that it utilizes its three declared properties for storing data on a particular user. However, it also transparently implements the eager loading pattern.

Think of this process very carefully: not only does the class declare the properties regardless of whether or not they're requested later, but it eagerly assigns some values to them. Naturally, there are valid reasons to perform this eager assignment that have to do with promoting good programming habits rather than with the eager loading pattern itself.

However, the script below shows that eager loading makes a lot of sense in this case:

require_once 'user.php'

// create instance of 'User' class

$user = new User();

// display user data

echo $user;

Simple to code and illustrative, right? As you can see, a direct call to the "__toString()" method of "User" will produce a non-empty output, which demonstrates how to apply eager loading to properties of a basic class.

So far, so good. Now that you hopefully understood how the previous example functions, it's time to continue learning a few more things regarding the application of lazy and eager loading patterns in PHP 5. Thus, as I stated in the introduction, in the following lines I'm going to show you how to use lazy loading for creating on the fly properties of a class. This will be a no-brainer process, believe me.

Now, to find out more on this topic, 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: