Welcome to the third part of a five-part series on lazy and eager loading in PHP 5. In this part of the series, you'll learn about the lazy design pattern, and how to use it effectively in your applications.
Very often, the terms "lazy" and "eager" are used in daily talking to define or qualify the way that certain people are, or simply to judge their behaviors and reactions in response to a number of situations that occur in life. The truth is that we, as individuals, are an interesting mixture of these two characteristics.
Also, it's true that human beings try to translate these concepts to other areas, such as software programming. If you've ever developed web-based or desktop applications using your favorite compiled or interpreted language, then it's possible that you've already coded "factory" methods to create one or multiple objects, or even defined a few "Observer" and "Dispatcher" modules as well.
Definitively, many design patterns like the ones mentioned above respond to simple translations of human behaviors within the terrain of software development, and certainly "lazy" and "eager" aren't the exception. In general, when an application uses lazy loading, it means that it delays or defers the loading of a specific resource until it's really required at runtime. On the other hand, if the same application (or a part of it) relies on eager loading, then it implies that the resource will be loaded as soon as possible, regardless of whether or not it has been requested.
Of course, it's possible to extend the concepts of "lazy" and "eager" when working with classes, which leads to using approaches commonly known as lazy and eager instantiations respectively. In the case of PHP 5, implementing lazy and eager loading is truly a no-brainer process, and the result of this implementation can be either successful or amazingly poor, depending on the context in which these patterns are applied.
Precisely, in the previous chapter of this series I developed an example that showed that there are times when including eagerly a class within a script can be pretty inefficient, especially if an instance of the class is created by the script in question right before it finishes its execution.
Nonetheless, it's possible to use the benefits of lazy loading to address an issue like this very easily, and that's exactly what I'm going to show you in the lines to come. So, if the topic interests you, start reading to find out more on it!