Home arrow PHP arrow 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.

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



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