Home arrow PHP arrow Page 4 - Checking Email Addresses with the Strategy Design Pattern

The EmailValidator class as a standalone component - PHP

In this fourth part of a series on the strategy design pattern, I extend the functionality of the sample validation program by adding another strategy class to it. The new class will check the validity of a supplied email address.

TABLE OF CONTENTS:
  1. Checking Email Addresses with the Strategy Design Pattern
  2. Review: validating input data with the Strategy design pattern
  3. Building a strategy class for validating email addresses
  4. The EmailValidator class as a standalone component
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
April 26, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As one might expect, using the email validator defined in the previous section as a standalone component is a straightforward process that can be tackled with minor efforts. In reality, checking the validity of a supplied email address via this concrete strategy class is reduced to coding a script similar to the one shown below:

// create an instance of the email validator class

$emailValidator = new EmailValidator('alejandrodomain.com');

// validate the supplied value

if (!$emailValidator->validate())

{

    echo $emailValidator->getFormattedError();

    /*

    displays the following

    The value alejandrodomain.com is incorrect. Please enter a valid email address..

      */

}

else

{

    echo ' The data that you entered is correct.';

}

There you have it. If you've ever built an object-oriented application that checks user-supplied email addresses, then the earlier code fragment should be pretty familiar to you. At the risk of being repetitive, keep in mind that the FILTER_VALIDATE_EMAIL constant used internally by the "filter_var()" PHP function only verifies whether or not the format of an email address is correct. So, if you need to implement more advanced features, such as looking for MX records in the DNS and checking to see if the supplied host is actually a working email server, you should consider adding this functionality on your own.

And with this final example, we've come to the end of this tutorial. As you saw before, utilizing the "EmailValidator" class in an isolated fashion is a breeze. However, things will become really interesting when I show you how to use it as part of a more complex validation strategy. This topic will be discussed in the next article.  

Final thoughts

In this fourth chapter of the series, I extended the functionality of the previous sample validation program by adding yet another strategy class to it. This new class was tasked with checking the validity of a supplied email address. With the incorporation of this modular component to the ones defined in preceding articles, you should now have a clear idea of how to inject them into the corresponding form helper class to assemble different validation strategies on the fly. But before I get to that point, it'd be useful to define at least one more strategy class that allows us to validate URLs in a simple way.

This will be the topic that I plan to discuss in the upcoming tutorial of the series, so stay tuned for more!



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