Home arrow PHP arrow Page 4 - Using Multiple Strategy Classes with the Strategy Design Pattern

Setting up a different validation strategy - PHP

Being one of the most popular contenders in the exciting, but very often complex, terrain of software design, the Strategy design pattern is a well-trusted paradigm. It allows you to build more flexible and efficient applications by appealing to the functionality of two of the big pillars of solid object-oriented programming: favoring Composition over Inheritance, and encapsulating the concepts that vary. This is the conclusion to a six-part series that shows you how to use the Strategy design pattern to validate incoming data.

  1. Using Multiple Strategy Classes with the Strategy Design Pattern
  2. Review: the example classes developed so far
  3. The Strategy pattern in action
  4. Setting up a different validation strategy
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
April 28, 2010

print this article



As I expressed in the past segment, it's ridiculously easy to implement an entirely different validation strategy using the set of classes created previously. For demonstration purposes, say that you need to determine if a couple of values collected via an HTML form are an integer and a valid URL respectively. In a case like this, the entire checking process should be performed in the following way:   


// include autoloader

require_once 'Autoloader.php';

$autoloader = Autoloader::getInstance();

// create an instance of the form helper

$formHelper = new FormHelper();

// add a couple of validators to the form helper

$formHelper->addValidator(new IntegerValidator(5))

           ->addValidator(new EmailValidator('alejandro@domain.com'));

// validate inputted data and display all error messages in one go

if (!$formHelper->validate())


    echo $formHelper->getErrorString();




    echo 'The submitted data is correct!';


    displays the following

       The submitted data is correct!



See how easy it is to switch over different validation strategies at run time? I bet you do! Since in this case the components responsible for checking inputted values are highly encapsulated objects, performing more complex (or stricter) validation is reduced to creating a few additional validator classes. It's that simple, really.

And with this final example, I'm finishing this humble series on implementing the Strategy design pattern in PHP. Now that you surely have a clearer idea of the logic that stands behind this popular pattern, you may want to use it for adding extra flexibility to your own web applications.

Final thoughts

It's hard to believe, but we've come to the end of this series. Hopefully the whole experience has been educational and even fun, as you learned how to implement the Strategy design pattern to tackle a real-world problem: validating incoming data in a truly flexible and modular way.

Taking into account the pattern's flat learning curve and the numerous benefits that it introduces into the application of good object-oriented programming techniques, you may want to consider implementing it in your coming PHP project.

Meet you in the next PHP tutorial!

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