0

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?

4
  • 1
    See answer on this question Commented Jan 31, 2015 at 17:33
  • 2
    The documentation is clear: The default value must be a constant expression, not (for example) a variable, a class member or a function call. Commented Jan 31, 2015 at 17:34
  • The only work around that I see is using callbacks. php.net/manual/en/language.types.callable.php Commented Jan 31, 2015 at 17:37
  • @samyismyhero Would you like to post an answer with implementation using callbacks? Not sure how you would do this. Commented Jan 31, 2015 at 17:40

3 Answers 3

4

Says it right in the docs, this cannot be done.

So instead it must be implemented like the following...

public static function sha512($token,$cost = 50000,$salt = null) {
    $salt = ($salt == null) ? (self::generateToken(16)) : ($salt);
    $salt = '$6$rounds=' . $cost . '$' . $salt . ' $';
    return crypt($token, $salt);
}
Sign up to request clarification or add additional context in comments.

Comments

0
public static function sha512($token,$cost = 50000,$salt) {
    $salt = self::generateToken(16);
    $salt = '$6$rounds=' . $cost . '$' . $salt . ' $';
    return crypt($token, $salt);
}

1 Comment

Welcome to Stack Overflow! It's highly recommended that you include some prose that helps others understand why or how your solution solves the question.
0

public static function sha512( $token, $cost = 50000, $salt = null )

more specifically the $salt = null

But I want to have the default value be the returned value from another function.

Keeping to this intent this is not possible in PHP, PHP does not support first-class functions. The only workaround that I see as I mentioned in my comment above was the use of callbacks.

From what I understand the $salt param is intended to use what ever salt that you might like or default to the default one in your code.

So here is a possible solution although I believe you already answered your own question:

public static function sha512( $token, $salt = null, $cost = 50000 ) {

    // if a custom salt is provided
    if( isset( $salt ) ) {

        // calls the callback function passed in as a param
         $salt = $salt( 16 );

        // assemble the result to your specs
        $salt = '$6$rounds=' . $cost . '$' . $salt . ' $';

        // return final result
        return crypt( $token, $salt );
    }

    // default if no custom salt is provided
    $salt = self::generateToken( 16 );
    $salt = '$6$rounds=' . $cost . '$' . $salt . ' $';

    // return final result
    return crypt( $token, $salt );
}

The passed in callback must be in the current scope to be called successfully.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.