Email text validator I'm using this pattern for the first time and wanted to check if this is the correct implementation.

class.validator.strategy.php

abstract class ValidatorStrategy
{
    abstract public function check( $name, $val );

}


class.text.validator.php

class TextValidator extends ValidatorStrategy
{

    public function check( $name, $val )
    {
         //logic here
    }
}


class.number.validator.php

class NumberValidator extends ValidatorStrategy
{

    public function check( $name, $val )
    {
         //logic here
    }
}


class.validator.php  

include('validator.strategy.php');
include('class.text.validator.php');
include('class.number.validator.php');
include('class.email.validator.php');

class Validator
{

    //holds strategy object
    protected $validatorStrategy = array();

    //holds form field
    protected $fields = array();

    public function __construct()
    {
        $this-&gt;validatorStrategy[ 'text' ] = new TextValidator();
        $this-&gt;validatorStrategy[ 'number' ] = new NumberValidator();
        $this-&gt;validatorStrategy[ 'email' ] = new EmailValidator();
    }

    public function ruleForTextField( $name, $value )
    {
        $this-&gt;fields[ $name ][ 'value' ] = $value;
        $this-&gt;fields[ $name ][ 'type' ] = 'text';
    }

    public function ruleForNumbertField( $name, $value )
    {
        $this-&gt;fields[ $name ][ 'value' ] = $value;
        $this-&gt;fields[ $name ][ 'type' ] = 'number';
    }

    public function ruleForEmailField( $name, $value )
    {
        $this-&gt;fields[ $name ][ 'value' ] = $value;
        $this-&gt;fields[ $name ][ 'type' ] = 'email';

    }

    public function isValidate()
    {
        $status = 0;

        foreach ( $this-&gt;fields as $key =&gt; $val )
        {
            if ( !$this-&gt;validatorStrategy[ $val[ 'type' ] ]-&gt;check( $key, $val ) )
            {
                $status++;
            }

        if ( $status == 0 )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}



Uses:

$validator = new Validator();
$validator-&gt;ruleForTextField('username', $_POST['username'] );
$validator-&gt;ruleForNumberField('age', $_POST['age'] );
$validator-&gt;ruleForEmailField('email', $_POST['email'] );

if($validator-&gt;isValidate())
{
    echo 'validated';
}
else
{
   echo 'fail validation';
}


Inside the isValidate() method, it iterate against all validator strategy and perform appropriate logic.

Is this is the correct way to use this pattern?
No, strategy pattern relies on an interface to allow composition to determine functionality at run time.  Notice that it is easier to extend with new speaking functionality; and can be used to execute at run time.  Here you are just using the array of the interface instantiations to do multiple validations.

I.E.

Interface for the pattern

public interface Speak {
  public void execute();
}


Implementations of the pattern

public class Quack extends Speak {
  public void execute() { /* Quack */ }
}

public class Bark extends Speak {
  public void execute() { /* bark */ }
}


Class that uses the strategy

public Animal {
  private Speak _speak;
  public Animal(Speak s) { _speak = s; }
}


Example usage

main() {
  new Animal(new Bark()); /* creates animal that barks */
  new Animal(new Quack()); /* creates animal that quacks */
}