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