HomePHP Building a Data Validation System with the Prototype Pattern with PHP 5
Building a Data Validation System with the Prototype Pattern with PHP 5
It's unusual to create multiple instances of a class, but certainly it may happen during the development of a PHP application. To solve this issue quickly and with minor hassles, the prototype design pattern can be really useful. If you want to learn more about it, this article might be what you need.
Welcome to the final part of the series that began with "The Basics of Using the Prototype Pattern with PHP 5." Comprised of two articles, this series shows you how to work with many instances of a given class using the remarkable functionality provided by the prototype pattern. It complements the theoretical concepts with a certain number of hands-on examples.
If you read the preceding article of the series, you're already familiar with the logic that drives the prototype pattern, since its implementation is extremely simple. Otherwise, if this pattern doesn't ring any bells for you, let me give you a brief explanation of how it functions.
Essentially, when the prototype pattern is applied in the context of a given PHP application, there's a class not surprisingly called "prototype" too, which is the original model followed by all the objects created from it. However, while this feature is common to all the conventional classes, the most interesting aspect of this pattern is that the first object created from the originating class is used to clone other objects, in this way giving us multiple instances of it.
As you can see, the prototype pattern has a rather uncommon implementation. That characteristic is precisely what makes it interesting from a developer's point of view, aside from its remarkable capacity for working with multiple instances of the same class.
Therefore, taking into account that this pattern can be helpful in situations where a prototype object is required, along with other clones of it, in this final installment of the series I'm going to demonstrate how to use it to build a simple application for checking the validity of incoming data.
I think you'll find this exercise to be both interesting and fun. So, come with me and let's learn together how to implement the prototype pattern in a more useful fashion. Let's begin now!