The best way to understand how the eager loading pattern is used by the sample “User” class is simply by coding a script that creates an instance of it without passing any incoming parameters to its constructor. The code sample that does that is the following, provided that the class in question has been saved to a file called “user.php”: require_once 'user.php' // create instance of 'User' class $user = new User(); // display user data echo $user; If you run this script on your own web server, the you’ll see that it outputs to screen the default values assigned to the “User” class properties, once an instance of it is created. This naturally happens due to the call to the magic “__toString()” method, but you’ll get a non-empty result when invoking any of its getter methods as well. While extremely simplistic, this example demonstrates that it’s possible to implement eager loading at lower levels and exploit its eventual benefits, even when in some cases these may be negligible. Feel free to edit all of the examples developed in this tutorial. Doing this surely will arm you with a better knowledge of eager loading in PHP 5. Final thoughtsIn this fourth installment of the series, I demonstrated through a simple example that it’s feasible to implement the eager loading pattern with the properties of a basic class. Nonetheless, the experience would be incomplete if I don’t show you how to apply lazy loading to the properties of the same class. That will be exactly the subject that I plan to discuss in the final part of this series, so I recommend that you not miss it!
blog comments powered by Disqus |