Command disabled: index


̃Gg[͂ĂȃubN}[Nɒlj

PHP :: abstractクラスを使ったバリデータの例



<?
abstract class abstractValidator
{
    abstract function Validate();
 
    protected $name;
    protected $value;
 
    public function __construct($name, $value)
    {
        $this->name  = $name;
        $this->value = $value;
    }
 
    public function getName()
    {
        return $this->name;
    }
 
    public function getValue()
    {
        return $this->value;
    }
}
 
class boolValidator extends abstractValidator
{
    public function Validate()
    {
        return is_bool($this->value);
    }
}
 
class intValidator extends abstractValidator
{
    public function Validate()
    {
        return is_int($this->value);
    }
}
 
 
 
$check = array(new boolValidator('a', 0), new intValidator('b', "hoge"));
$msg = "";
foreach ($check as $c) {
    if ( ! $c->Validate()) $msg .= '[invalid variable] name: '.$c->getName().' / value: '.$c->getValue()."\n";
}
echo $msg;
 
?>
  • 実行結果
[invalid variable] name: a / value: 0
[invalid variable] name: b / value: hoge



programming/php/etc/abstract_validator.txt