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

Completing the User 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
 

In the previous segment, I started building a trivial class whose functionality was reduced to storing only data on some hypothetical users. The definition of this class is still incomplete, though, since it’s necessary to add to it a few getter methods that allow it to retrieve the values assigned to its properties.

Indeed, coding these additional methods within the class is as simple as this:

// 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;

}

Done. Now the above “User” class provides a basic, yet consistent, API that permits it to retrieve easily the value of its “fname,” “lname” and “email” properties. In addition, I implemented the magic “__toString()” method included with PHP 5, which comes in handy for outputting those properties to the screen in one single go.

After adding the previous getter methods, the finished version of the “User” class looks like this:

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;

}

}

 

All right, I have to admit that until now nothing special has happened, since all I did was build a basic class that can be used for storing data on some users. However, the question that comes up here is: where does the eager loading pattern fit in the context of this example? I asked you before to be patient, and hopefully you’ll be properly rewarded.

Now that the “User” class is available for testing purposes, the next step that must be taken is developing a script that uses it for implementing eager loading.

Want to see how this script will be built? Then 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: