Home arrow PHP arrow Page 2 - Validating Incoming Data by Using Polymorphism with Objects in PHP 5

Using polymorphism to validate input data - PHP

If you're a PHP developer who wants to learn how to take advantage of polymorphism to build more efficient and robust object-oriented applications, then this group of articles might be what you need. Welcome to the final part of the series that started with "Using Polymorphism with Objects in PHP 5." Comprised of three tutorials, this series shows you how to create polymorphic classes with PHP; it also teaches you how to use them in real-world situations.

  1. Validating Incoming Data by Using Polymorphism with Objects in PHP 5
  2. Using polymorphism to validate input data
  3. Extending the implementation of polymorphism
  4. Validating user-supplied data by using polymorphic objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 04, 2007

print this article



A good place to start demonstrating how a group of polymorphic classes can be used to validate user-supplied data is with building a base abstract class. This class will define only one generic method, in this case called "validate()," and aimed at validating different types of input data.

After defining this parent class, I'll derive a few concrete subclasses which will implement concretely the aforementioned "validate()" method, so each of them can be used to validate specific types of incoming data. Now, do you see how polymorphism fits into this schema? I'm sure you do!

All right, now that I have explained how I plan to create a data validation application using some polymorphic classes, please take a look at the signature of the corresponding parent, which as I said before, has been declared abstract. Its short definition is as follows:

// define 'DataValidator' class
abstract class DataValidator{
   abstract public function validate($inputData);

As you can see, the above "DataValidator" class acts simply as an interface for creating all the corresponding subclasses that will be responsible for validating specific types of incoming data. In this case in particular, notice that the class only exposes an abstract "validate()" method, but this should give you at least a vague idea of how the classes that will created later on will implement concretely the method in question.

Okay, now that you know how the base class looks, it's time to start deriving some concrete classes from it so they're able to validate different types of user-supplied data.

Having said that, here are the definitions that correspond to the first four subclasses. They are tasked with checking empty strings first, then integers and numeric data, and finally ranges of values.

The respective signatures for these brand new classes are as follows:

// define 'EmptyValidator' class
class EmptyValidator extends DataValidator{
const MIN=4;
const MAX=32;
public function validate($inputData){
return false;
return true;

// define 'IntegerValidator' class
class IntegerValidator extends DataValidator{
public function validate($inputData){
return false;
return true;

// define 'NumericValidator' class
class NumericValidator extends DataValidator{
public function validate($inputData){
return false;
return true;

// define 'RangeValidator' class
class RangeValidator extends DataValidator{
const MIN=1;
const MAX=99;
public function validate($inputData){
return false;
return true;

As shown above, the data validating classes listed previously are an excellent example of how to use the functionality provided by polymorphism to check a broad range of incoming data. This concept becomes clear since all the prior classes belong to the same "DataValidator" family, but in each case they implement the "validate()" method in a different way. Pretty good, isn't it?

Okay, at this point I'm positive that you already grasped the logic that stands behind this "polymorphic" data validation system, so it's time to move forward and continue defining some additional subclasses. In this way the initial capacity of the system in question can be greatly expanded.

To see how these brand new data checking classes will be built, please click on the link shown below and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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