james_mckinnon
james_mckinnon

Reputation: 65

Unexpected PHP Parse Error

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.

Upvotes: 2

Views: 81

Answers (2)

Brandon - Free Palestine
Brandon - Free Palestine

Reputation: 16656

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:

Upvotes: 6

Nadeem Khan
Nadeem Khan

Reputation: 3424

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;

};
}

Upvotes: 3

Related Questions