Home arrow PHP arrow Page 3 - Building a Data Validation System with the Prototype Pattern with PHP 5

Creating a few additional classes - 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.

TABLE OF CONTENTS:
  1. Building a Data Validation System with the Prototype Pattern with PHP 5
  2. Building a simple data validation system
  3. Creating a few additional classes
  4. Creating an example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
May 22, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: