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