HomePHP The Basics of Using the Prototype Pattern with PHP 5
The Basics of Using the Prototype Pattern with PHP 5
The prototype class lets you use many instances of a specific class, without copying objects to different variables. This article, the first of a two-part series, demonstrates the basic functionality provided by this pattern.
If you're a PHP developer who has been building web applications with the object-oriented paradigm for a while, then you'll have a solid background in how to create classes and define their properties and methods, in addition to handling their instantiation in accordance with the business logic implemented by your PHP scripts.
Now that I mentioned the term "class," surely it'll be familiar to you that this programming structure is in fact a blueprint. This blueprint is used for all the eventual objects created from it, which logically will expose a behavior consistent with the methods defined by the class in question.
Expressed in simple terms, when you define a class in the context of a given PHP application, what you're actually doing is creating a prototype model that will be implemented by one or more instances of the originating class. Naturally there will be some variations, particularly if you're working with subclasses that override and overload methods defined by the respective parent.
However, there's one thing in particular that I'd like to analyze in detail concerning the definition expressed in the previous sentences. As you may have noticed, I said that it was possible to work with more than one instance of a class, even though this circumstance may happen rather infrequently.
Logically, if this condition ever occurs, it'll be necessary to build a mechanism that allows one to easily deal with multiple instances of one class, without having to complicate the source code of a certain application with complex, and sometimes inefficient, programming methodologies.
The situation described above is the perfect scenario for implementing the prototype pattern, since it will let you use many instances of a specific class, without copying objects to different variables. At this time you may be wondering how this can be achieved. Well, in plain terms when the prototype pattern is applied, the first instance of a class is used as a prototype model, and all the other instances of the class in question are created by cloning the mentioned prototype object.
Although at first glance the definition of the prototype pattern seems a bit complicated, the truth is that it can be implemented by a few functional PHP code samples very easily. That will be the main goal of this two-part series. More concretely speaking, in this first tutorial of the series I'm going to demonstrate only the basic functionality provided by this pattern, while in the second article I'm going to use the pattern in a more useful situation, that is building an extensible data validation system.
Now that you know the wealth of interesting topics that will be covered in this series, let's start learning the key concepts of the prototype pattern. Let's begin this educational journey now!