Ideally I would like to do something like this….
$formElement->addValidator
(
(new RegexValidator('/[a-z]/') )->setErrorMessage('Error')
// setErrorMessage() returns $this
);
Of course PHP won’t allow that, so I settle for this…
$formElement->addValidator
(
RegexValidator::create('/[a-z]/')->setErrorMessage('Error')
);
And the code in the Base class….
static public function create( $value )
{
return new static( $value );
}
I would like to go one step further and do something like this…
static public function create()
{
return call_user_func_array( 'static::__construct', func_get_args() );
}
Again, PHP won’t allow me to do this. I could code individual ‘create’ methods for each validator, but I want it to be a little more slick.
Any suggestions please?
Use
ReflectionClass::newInstanceArgsfrom Reflection API: