HomePHP Page 2 - Building a Data Validation System with the Prototype Pattern with PHP 5
Building a simple data validation system - PHP
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.
As I said right in the beginning of this article, I'm going to demonstrate how to implement the prototype pattern by creating a simple mechanism for validating input data. As you'll see, this data checking application will be capable of working with many instances of a specific object, which will be used to verify the validity of certain data types, such as email addresses, alphanumeric and alphabetic values, etc.
Okay, having explained how I plan to apply the prototype pattern in this particular situation, please have a look at the signatures of the following prototype classes. They establish a generic structure for validating alphanumeric and alphabetic strings.
Here are the prototype classes:
// define abstract 'DataValidatorPrototype' class abstract class DataValidatorPrototype{ abstract public function validateData($inputData); abstract public function __clone();
}
// define concrete 'AlphabeticValidatorPrototype' class class AlphabeticValidatorPrototype extends DataValidatorPrototype{ public function validateData($inputData){ if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){ return false; } return true; } public function __clone(){} }
// define concrete 'AlphanumericValidatorPrototype' class class AlphanumericValidatorPrototype extends DataValidatorPrototype{ public function validateData($inputData){ if(!$inputData||!preg_match("/^[a-zA-Z0-9]+$/",$inputData)){ return false; } return true; } public function __clone(){} }
As illustrated above, the three classes are very convenient for checking certain types of data, and come in handy for working with alphabetic and alphanumeric values.
Of course, you'll realize that the first abstract class, the one called "DataValidatorPrototype," is used to define the generic characteristics of any data validator object that might be potentially derived from it. However, aside from studying in detail the functionality provided by these classes, you should notice that each of them declares a magic "__clone()" method.
This is very relevant, since the intrinsic definition of the prototype pattern says that all the objects spawned from the prototype will be created via a cloning process. Thus, that's exactly the reason for declaring the "__clone()" method. Quite simple, right?
All in all, I defined two concrete classes for validating alphabetic and alphanumeric data. Considering that I'd like to extend the initial capacity of this data checking application, in the section to come I'm going to define a few additional prototype classes. They will be tasked with validating numbers and email addresses as well.
To see how these brand new classes will be built, click on the link below and keep reading.