Building a Data Validation System with the Prototype Pattern with PHP 5 - Creating a few additional classes
(Page 3 of 4 )
As you'll certainly recall from the previous section, my intention here is to extend the functionality of this simple data validation application to make it capable of checking some additional incoming data, like numbers and email addresses.
Logically, these brand new classes will also declare the magic "__clone()" method that you saw earlier, so they can clone objects that might be needed by an application.
Now that I've clarified how these data checking classes are going to work, please take a minute and examine their respective signatures, which look like this:
// define concrete 'NumbericValidatorPrototype' class
class NumbericValidatorPrototype extends DataValidatorPrototype{
public function validateData($inputData){
if(!$inputData||!is_numeric($inputData)){
return false;
}
return true;
}
public function __clone(){}
}
// define concrete 'EmailValidatorPrototype' class
class EmailValidatorPrototype extends DataValidatorPrototype{
public function validateData($inputData){
if(!$inputData||!preg_match("/.+@.+\..+./",$inputData)||!
checkdnsrr(array_pop(explode("@",$inputData)),"MX")){
return false;
}
return true;
}
public function __clone(){}
}
As you can see, the prototype classes listed above implement the same business logic present in their predecessors concerning the declaration of the "__clone()" method. The definition of the method has been done deliberately, providing each validation class with the capacity to clone objects across a given application.
So far, so good. At this point hopefully you've grasped the functionality of each of the prototype classes defined previously. However, I'm pretty certain that you're asking yourself…how can the prototype pattern be linked with the classes in question?
Well, to answer that smart question, in the section to come I'm going to develop a short script. It will show how multiple instances of the data checking classes that you have just learned can be put to work in conjunction.
Hopefully, after you see the example, you'll have a more polished concept of how the prototype pattern functions.
To see how this practical example will be developed, please jump ahead and read the next few lines. I'll be there, waiting for you.
Next: Creating an example >>
More PHP Articles
More By Alejandro Gervasio