【问题标题】:PHP function error T_FUNCTIONPHP函数错误T_FUNCTION
【发布时间】:2010-11-24 18:20:50
【问题描述】:

我在第 4 行遇到错误

解析错误:语法错误,第 21 行 C:\xampp\htdocs\work\CASC\admin\form-validator.php 中的意外 T_FUNCTION

有人可以帮忙吗?

public function email($message='')
    {
        $message = ( empty ($message) ) ? '%s is an invalid email address.' : $message;
        $this->set_rule(__FUNCTION__, function($email) {
            return ( filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE ) ? FALSE : TRUE;
        }, $message);
        return $this;
    }


private function set_rule($rule, $function, $message='')
    {
         // do not attempt to validate when no post data is present
        if ( $this->haspostdata ) {
            if ( ! array_key_exists($rule, $this->rules) ) {
                $this->rules[$rule] = TRUE;
                if ( ! array_key_exists($rule, $this->functions) && is_callable($function) ) {
                    $this->functions[$rule] = $function;
                }
                if ( !empty ($message) ) {
                    $this->messages[$rule] = $message;
                }
            }
        }
    }

【问题讨论】:

  • set_rule() 期望作为第二个参数关闭?
  • 我已编辑问题,请立即查看

标签: php class function


【解决方案1】:

您的代码完全有效。该错误听起来像是您没有使用 PHP5.3 运行它,这是使用闭包时所必需的。

5.3 之前的方式是:

private function emailRule($email)
{
    return ( filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE ) ? FALSE : TRUE;
}

public function email($message='')
{
    $message = ( empty ($message) ) ? '%s is an invalid email address.' : $message;
    $this->set_rule(__FUNCTION__, array($this, 'emailRule'), $message);
    return $this;
}

【讨论】:

  • 是的,这就是问题所在。有什么选择?
  • @g.b.:没有合理的选择。 (不!create_function 不是替代品!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 2010-11-20
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
相关资源
最近更新 更多