2

I realise that the answer to this question is probably going to be simply obvious, but I am stumped.

I've created a class that uses Jeremy Kendall's Password Validator (https://github.com/jeremykendall/password-validator) called Hash. However, when the class gets called I get:

Parse error: syntax error, unexpected '}' in /home/james/Projects/REC/htdocs/classes/Hash.class.php on line 24.

The code in Hash.class.php that it is having issues with is:

private $validationCallback;

public function __construct(){
    $this->validationCallback = function($credential, $passwordHash){
        if (has('md5', $credential) === $passwordHash) {
            return true;
        }
        return false;
    }
}

Line 24 is the final '}'. Any help would be greatly appreciated.

Thanks.

1
  • MD5? Lordie, do they still use that in the 21st century? Commented Apr 25, 2014 at 11:58

2 Answers 2

6

This should be:

private $validationCallback;

public function __construct(){
    $this->validationCallback = function($credential, $passwordHash)
{
    if (has('md5', $credential) === $passwordHash) {
        return true;
    }
    return false;
}; // Missing semicolon needs to be here
}

Please see PHP's Anonymous Function documentation:

Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon:

3

Anonymous functions need a semicolon at the end. Try this:

private $validationCallback;

public function __construct(){
$this->validationCallback = function($credential, $passwordHash)
{
if (has('md5', $credential) === $passwordHash) {
    return true;
}
return false;

};
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.