Home arrow PHP arrow Page 3 - Validating User Input with the Strategy Pattern

Building some strategy classes - PHP

The strategy design pattern is applied much more often than you might think, so if you want to find out how to implement it with PHP 5, this article should guide you through the whole learning process. Welcome to the final installment of the series that began with “Introducing the Strategy Pattern.” In two parts, this series walks you through the key points of how the strategy pattern works, and accompanies its theoretical concepts with copious hands-on examples.

  1. Validating User Input with the Strategy Pattern
  2. Building a validation strategy selector
  3. Building some strategy classes
  4. Validating user-supplied data
  5. Listing all the classes required to implement the strategy pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
March 06, 2007

print this article



As you'll certainly recall from the previous section, the validation strategy selector spawns four different objects, and each one of them is responsible for validating a specific type of data. Therefore, taking this situation into account, below I listed the respective signatures corresponding to these strategy classes.

Here they are, so have a look at them, please:

// define 'StrategyAlphabetic' class
class StrategyAlphabetic{
   public function validateData($inputData){
       return false;
     return true;
// define 'StrategyAlphanumeric' class
class StrategyAlphanumeric{
    public function validateData($inputData){
        return false;
      return true;
// define 'StrategyNumber' class
class StrategyNumber{
   public function validateData($inputData){
       return false;
     return true;
// define 'StrategyEmail' class
class StrategyEmail{
   public function validateData($inputData){
       return false;
     return true;

As shown above, each of the four strategy classes listed previously implements differently the same "validateData()" method to check a diverse range of user inputs, such as alphabetic and alphanumeric values, and number and email addresses.

Of course, it's possible to extend the initial validation capacity of the whole data checking system even more, simply by adding new strategy classes to the existing ones. This is definitely a process that can be performed with minor hassles.

So far, so good. At this point, you've seen not only how the validation strategy selector looks, but how the four strategy classes have been properly defined. This is very convenient for understanding the schema imposed by the strategy pattern.

So, the question that comes up is: what's the next step now? Well, considering that you already grasped the logic implemented by all the classes that I defined so far, it's time to move forward and see a concrete example where a data validation system is created by using the aforementioned classes.

Want to see how the strategy pattern is applied to validate user-supplied input? Jump straight into the following section 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: