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

Building a strategy class for validating email addresses - 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.

  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



As you may have guessed, defining a new strategy class that validates email addresses is very similar to creating components that check for integer and float numbers. The entire process is reduced to deriving a concrete subclass from the parent "AbstractValidator" and overriding its "$_filter" and "$_errorMessage" properties respectively. It's that simple, really.

The following code fragment shows the definition of the aforementioned subclass, not surprisingly called "EmailValidator":



class EmailValidator extends AbstractValidator


    protected $_filter = FILTER_VALIDATE_EMAIL; 

    protected $_errorMessage = 'Please enter a valid email address.';


What can I say about the above "EmailValidator" class that hasn't already been said? Well, not much really, except for the fact that in this case it uses the FILTER_VALIDATE_EMAIL constant to determine whether or not a given email address is properly formatted. Since this PHP filter doe not also check to see if the domain part of the address is a valid host, or if it has an associated MX record in the DNS, adding this functionality is up to you. You've been warned.

Okay, having already defined a new strategy class that can be used for checking email addresses, the next logical step is to test it as a standalone component, so you can see for yourself how easy it is to put it in action.

But guess what? This will be done in the section to come. To get there, click on the link 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: