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

Extending the implementation of polymorphism - 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

As I stated in the section that you just read, below I included a new group of data checking classes, created specifically for validating alphabetic and alphanumeric values, as well as email addresses. You should notice that I also created an additional class that verifies the validity of a given email address that can be used with Windows-based machines, in case you're working with that operating system.

Having clarified that, here are the signatures that correspond to the aforementioned classes. They look like this:

// define 'AlphabeticValidator class
class AlphabeticValidator extends DataValidator{
   public function validate($inputData){
    
if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){
      
return false;
    
}
    
return true;
  
}
}

// define 'AlphanumericValidator' class
class AlphanumericValidator extends DataValidator{
  
public function validate($inputData){
    
if(!$inputData||!preg_match("/^[a-zA-Z0-9]+$/",$inputData)){
      
return false;
    
}
    
return true;
  
}
}

// define 'EmailValidator' class
class EmailValidator extends DataValidator{
  
public function validate($inputData){
    
if(!$inputData||!preg_match("/.+@.+..+./",$inputData)||!
checkdnsrr(array_pop(explode("@",$inputData)),"MX")){
      
return false;
    
}
    
return true;
   
}
}

// define 'EmailValidatorWin' class
class EmailValidatorWin extends DataValidator{
  
public function validate($inputData){
    
if(!$inputData||!preg_match("/.+@.+..+./",$inputData)||!
$this->windnsrr(array_pop(explode("@",$inputData)),"MX")){
      
return false;
    
}
    
return true;
  
}
  
private function windnsrr($hostName,$recType=''){
    
if($hostName){
      
if($recType=='')$recType="MX";
      
exec("nslookup -type=$recType $hostName",$result);
      
foreach($result as $line){
        
if(preg_match("/^$hostName/",$line)){
          
return true;
        
}
      
}
      
return false;
     
}
    
return false;
  
}
}

As you can see, the above data validation classes look very similar to the ones created in the previous section. Logically, the only difference to spot here is with reference to the diverse implementations of the respective "validate()" method. It's different for each class.

Again, you'll realize that I'm playing with the concept of polymorphism to expand the capacity of this data checking application, something that you can try for yourself if you want to verify additional types of data. As you learned previously, the process is indeed simple.

Well, at this stage I have built a decent number of data checking classes for validating a broad range of user-supplied entries by taking advantage of polymorphism. However, there's still a missing piece in this schema, since a programmatic mechanism is required here to demonstrate more clearly how to use the polymorphic facet exposed by all these classes.

So, what is that piece? In short, I plan to use a factory class so I can easily spawn a certain number of validating objects. These objects will be used to check different types of user-supplied data. Of course, I'll keep the source code of this factory class extremely simple, because I don't want to bother you with irrelevant details. Here's how this class looks:

// define 'ValidatorFactory' class
class ValidatorFactory{
  
public function createValidator($validator){
    
return new $validator();
   
}
}

The above factory class returns to client code a specific kind of validating object, in accordance with the incoming $validator parameter passed to its respective "createValidator()" method.

So far, so good. At this point, I have shown you the complete signatures that correspond to all the validation classes that you learned before, in conjunction with an additional factory class. Therefore, the next step will consist of developing a hands-on example, where you'll see how all these classes can be put to work together.

Want to see how this practical example will be developed? Keep reading, please.



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