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

Validating user-supplied data by using polymorphic objects - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In accordance with the concepts that I deployed in the previous section, here is an illustrative example that shows how to use some of the data checking classes that you learned before. This will demonstrate how useful and powerful polymorphism can be.

Take a look at the following code sample, please:

try{
 
$valFactory=new ValidatorFactory();
 
// create 'EmptyValidator' object
 
$emptyVal=$valFactory->createValidator('EmptyValidator');
 
if($emptyVal->validate('')){
   
echo 'Input data is valid!';
 
}
 
else{
   
echo 'Input data is not valid!';
 
}
 
// create 'IntegerValidator' object
 
$integerVal=$valFactory->createValidator('IntegerValidator');
 
if($integerVal->validate(1000)){
   
echo 'Input data is valid!';
 
}
 
else{
   
echo 'Input data is not valid!';
 
}
 
// create 'NumericValidator' object
 
$numericVal=$valFactory->createValidator('NumericValidator');
 
if($numericVal->validate('ABCD')){
   
echo 'Input data is valid!';
 
}
 
else{
   
echo 'Input data is not valid!';
 
}
 
// create 'AlphanumericValidator' object
 
$alphanumericVal=$valFactory->createValidator
('AlphanumericValidator');
 
if($alphanumericVal->validate('ABCD123456')){
   
echo 'Input data is valid!';
 
}
 
else{
   
echo 'Input data is not valid!';
 
}
 
// create 'AlphabeticValidator' object
 
$alphabeticVal=$valFactory->createValidator
('AlphabeticValidator');
 
if($alphabeticVal->validate('ABCD')){
   
echo 'Input data is valid!';
 
}
 
else{
   
echo 'Input data is not valid!';
 
}
}
catch(Exception $e){
 
echo $e->getMessage();
 
exit();
}

As you can see, the sample script shown above demonstrates in a friendly fashion how to use some of the classes created previously to validate different types of data. The respective objects display a primitive message indicating whether or not a particular value is valid, but this can be easily changed to fit your personal needs.

As usual, feel free to tweak the source code of all the classes shown in this article, so you can eventually acquire a better understanding of how polymorphism can be used to build more efficient PHP applications.

Final thoughts

Finally, we've come to the end of this series. Hopefully, after reading these three tutorials, you'll have a more accurate idea of how to take advantage of the functionality provided by polymorphism to build more solid and robust object-based PHP applications.

As expressed earlier, polymorphism can be applied successfully in those situations where you need to work with a bunch of objects that belong to the same family, but eventually can have different behaviors.

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: