开发者

PHP 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_开发者_StackOverflow中文版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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜