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

Applying lazy loading to class properties - 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 order to illustrate how lazy loading can also be used successfully with the properties of a class, I'm going to redefine the previous "User" class so it can create properties dynamically. 

Having explained this, take a look at the brand new definition of "User," which now is as follows:

class User

{

private $data = array();

 

// constructor (not implemented)

public function __construct(){}

 

// implement '__set()' magic method

 public function __set($name, $value)

{

if (!isset($this->data[$name]))

{

$this->data[$name] = $value;

}

}

 

// implement '__get()' magic method

public function __get($name)

{

if (isset($this->data[$name]))

{

return $this->data[$name];

}

}

 

// implement '__toString()' magic method

public function __toString()

{

$str = '';

foreach ($this->data as $property => $value)

{

$str .= 'Property: ' . $property . ' Value: ' . $value . '<br />';

}

return $str;

}

}

For obvious reasons, building a class that's only comprised of a few magic PHP 5 methods isn't a good thing at all, but given that I plan to use it for demonstrative purposes I recommend that you study its signature closely to grasp its underlying logic.

As shown above, apart from implementing the already familiar "__toString()" method, the class does the same thing with the complementary "__set()" and "__get()" tandem, which permits it to dynamically create and retrieve undeclared properties, a process commonly known in PHP 5 as property overloading.

It's quite possible that at this moment you're wondering what the point is in implementing the "__set()" and "__get()" magic methods. Considering that they can be used for creating and fetching properties on request, then it'd be correct to say that those properties could be brought to life in a true "lazy" way. That's a quite interesting concept, isn't it?

Of course, in most cases using property overloading can make the code of classes difficult to read and harder to follow. However, in this particular example this approach comes in handy for demonstrating how easy it is to use lazy loading when creating the properties of a basic class.

But if you're like me, you'll want to see an example that shows how to work with the modified version of the previous "User" class. Therefore, in the course of the section to come I'm going to build a simple script that will lazily assign properties to that class.

To see how this script will be developed, 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
   

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: