In this article, the first of a three-part series, you will be introduced to the basics of creating directors and builder objects with PHP 5. As usual, there will be copious examples to help you quickly start using the builder pattern in your own PHP projects.
In PHP, as with many other trusted programming languages out there, there are many ways to establish diverse relationships between objects during the development of an application. This concept has an immediate implementation in well-known circumstances within the context of object-based programming, particularly in those cases where both aggregation and composition play a relevant role.
More specifically, if you’ve been using classes inside your PHP applications for a while, then you’ll know that there are several programming environments where a particular object isn’t completely independent. It requires the “assistance” of one or many additional objects to perform a given task.
As I expressed earlier, composition and aggregation are clear examples of well-defined object interaction, but they're definitely not the only ones that exist in the object-based universe. Even without having a thorough theory that backs you up (using only your common sense), certainly you can conceive of numerous ways for two or more objects to perform a mutual (and productive) interaction, with no need to read thick books on object-oriented programming.
Now, and particularly when using PHP 5, I’d like to introduce a concept that’s strongly related to a specific interaction between multiple objects. In this case, I’m talking about directors and builder objects, which when evaluated in conjunction, conform to what is commonly known as the “builder” pattern. But right now you’re probably wondering how the builder pattern works.
Well, to put things in a simple perspective, when the builder pattern is applied, two objects are put into action to create a third one. First, these two objects create a director; as its name suggests, the director controls all aspects of the creation process and determines what pieces and modifications will be introduced into the target object. Finally, a builder is tasked with constructing the third object in question, in accordance with particular specifications. Sounds fairly simple, doesn’t it?
Since you might have to address specific issues by applying the mentioned builder pattern eventually, in this series you’ll learn how to use it, and of course how to get the most out of it.
Are you ready to learn more on how to use directors and builder objects in PHP? Let’s get started!