考虑我的贫困阶层:
abstract class FormValidator
{
private $error_objects = array();
protected function setError($entry_name,$err_msg)
{
$this->error_objects[] =
new FormValidatorErrorObject($entry_name,$err_msg);
}
protected function setErrorCurry($entry_name)
{
$_this = $this;
return function($err_msg) use($entry_name,$_this)
{
return $_this->setError($entry_name,$err_msg);
};
}
public function countErrors()
{
return count($this->error_objects);
}
public function getError($index)
{
return $this->error_objects[$index];
}
public function getAllErrors()
{
return $this->error_objects;
}
abstract function validate();
}
我在实现类中使用它,如下所示:
$setError = $this->setErrorCurry('u_email');
if(empty($uemail))
{
$setError(uregform_errmsg_email_null);
}
if(!filter_var($uemail,FILTER_VALIDATE_EMAIL))
{
$setError(uregform_errmsg_email_invalid);
}
,这会导致以下错误:
Fatal error: Call to protected method FormValidator::setError() from context '' ...
问题:有没有办法让闭包“继承”类上下文?