In PHP you can have optional parameters for a function:
public static function test($var = 'default string')
But I want to have the default value be the returned value from another function. For example I have a method called generateToken() that creates a random string of desired length. I attempted to execute the following code...
public static function sha512($token,$cost = 50000,$salt = self::generateToken(16)) {
$salt = '$6$rounds=' . $cost . '$' . $salt . ' $';
return crypt($token, $salt);
}
However this throws the following error...
Parse error: syntax error, unexpected '(', expecting ')' in /var/www/www.domain.com/path/to/cryptography.php on line 11
Line 11 is the method deceleration line. I am aware this could be done by setting the default to something like 'NONE' and then having an if statement call the method, but I would like to do it inside the method deceleration. How can I do this?
The default value must be a constant expression, not (for example) a variable, a class member or a function call.