I'm trying to directly call a function from a static property inside a class.
Here is an extract of my class:
class Uuid {
const VERSION_3 = 3;
const VERSION_5 = 5;
protected static $hash_function = [
self::VERSION_3 => 'md5',
self::VERSION_5 => 'sha1',
];
protected static function get_hash_value($value_to_hash, $version) {
// None of these work:
//$hash = self::$hash_function[$version]($value_to_hash);
//$hash = (self::$hash_function[$version])($value_to_hash);
//$hash = (self::$hash_function)[$version]($value_to_hash);
// Only this works:
$function = self::$hash_function[$version];
$hash = $function($value_to_hash);
return $hash;
}
}
The only way I've found of making it work so far is to store the function name in a temporary variable ($function
) before calling it. I've tried wrapping the expression (or bits of the expression) in braces, ({
,}
), parentheses ((
,)
), prefixing a $
, etc. but so far nothing has worked.
Is there a simple way of doing this without a temporary variable? If so, what is the minimum version of PHP this works for?