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.
blog comments powered by Disqus |
|
|
|
|
|
|
|