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

Creating an example - 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 I said in the section that you just read, demonstrating the functionality of the prototype pattern, in this case implemented as part of a basic data validation application, is only a matter of building a simple PHP script that puts all the prototype classes to work together.

Basically, what I'm going to illustrate here is that it's possible to work with a cloned instance of a specific validation object to check whether or not a particular input entry is considered offending. Naturally, the proper verification of incoming data will be performed by implementing the programmatic model dictated by the prototype pattern. In this way you can see more clearly how it works in this concrete case.

All right, now that I have explained the purpose of coding a hands-on example, please pay attention to the following code listing. It shows this handy pattern in action. Here it is:   

try{
   // create new 'AlphabeticValidatorPrototype' object
   $alphaPrototype=new AlphabeticValidatorPrototype();
   // clone prototype object
   $alphaVal=clone $alphaPrototype;
   // validate input data
   if(!$alphaVal->validateData('ABCDE')){
     echo 'Input data is not valid!';
   }
   else{
     echo 'Input data is valid!';
   }

   /* displays the following:
   Input data is valid!
   */

   // create new 'AlphanumericValidatorPrototype' object
   $alphanumPrototype=new AlphanumericValidatorPrototype();
   // clone prototype object
   $alphanumVal=clone $alphanumPrototype;
   // validate input data
   if(!$alphanumVal->validateData('****12345***')){
     echo 'Input data is not valid!';
   }
   else{
     echo 'Input data is valid!';
   }

   /* displays the following:
   Input data is not valid!
   */

   // create new 'NumericValidatorPrototype' object
   $numPrototype=new NumbericValidatorPrototype();
   // clone prototype object            
   $numVal=clone $numPrototype;
   // validate input data
   if(!$numVal->validateData('12345')){
     echo 'Input data is not valid!';
   }
   else{
     echo 'Input data is valid!';
   }

   /* displays the following:
   Input data is valid!
   */
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Wasn't the above example easy to grasp? I bet it was! As you can see, the example first creates the corresponding prototype objects associated with every data checking class, and then uses the PHP built-in "clone()" method to create one instance of them. Next, these cloned instances perform a simple validation process on a bunch of primitive sample data, and finally an indicative message is displayed on the browser according to the results of this procedure.

A final note before you finish reading this article: I recommend that you use all of the prototype classes that I included here as a starting point for understanding more clearly how the prototype pattern works. Of course, you may want to introduce your own modifications to the classes or even better, develop your own functional examples.

As with many other topics associated specifically to PHP programming, it's the best way to learn. 

Final thoughts

Unfortunately, we've come to the end of this series. I hope that all the examples shown in this group of articles will be useful enough to demonstrate in a friendly fashion how the prototype pattern can be implemented with PHP 5.

As you have seen, there are design patterns that are more unusual than others, and this is precisely the case with the prototype pattern. However, it doesn't hurt to learn at least its basic principles, and eventually how it can be used to solve specific programming-related issues.

See you in the next PHP tutorial!



 
 
>>> 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: