PHP 致命错误:从上下文调用受保护方法 FormValidator::setError()
考虑我可怜的类:
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 '' ...
问题:有没有办法使闭包“继承”类上下文?
consider my poor class:
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();
}
I use it in the implementing class like this:
$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);
}
and that results in the following error:
Fatal error: Call to protected method FormValidator::setError() from context '' ...
Question: is there a way to make the closure "inherit" the class context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然不是原生的。 本手册说明建议使用反射和包装类为闭包提供私有/受保护的访问功能。
Apparently not natively. This manual note suggests a rather cumbersome way of using reflection and a wrapper class to give closures private/protected access functionality though.