Gajendra Bang
Gajendra Bang

Reputation: 3583

PHP function error T_FUNCTION

Im getting error on line 4

Parse error: syntax error, unexpected T_FUNCTION in C:\xampp\htdocs\work\CASC\admin\form-validator.php on line 21

anyone can help?

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;
                }
            }
        }
    }

Upvotes: 0

Views: 180

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318498

Your code is perfectly valid. The error sounds like you are not running it using PHP5.3 which is required when using closures.

The pre-5.3 way would be:

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;
}

Upvotes: 4

Related Questions