PHP : ReflectionParameter, isOptional vs isDefault

2020-02-09 05:03发布

问题:

What is the difference between two. both of these are working exactly in a same way.

public static function getArgsArray($reflectionMethod,$argArray){
    $arr = array();
    foreach($reflectionMethod->getParameters() as $key => $val){
        $arr[$val->getName()] = isset($argArray[$val->getName()]) ?
        $argArray[$val->getName()] : (isset($_REQUEST[$val->getName()])
                ? $_REQUEST[$val->getName()] : ($val->*isDefaultValueAvailable()*  ? $val->getDefaultValue() : NULL));
    }
    return $arr;
}

回答1:

Good question. Consider this example

function foo($foo = 'foo', $bar) {}

For the $foo parameter, isDefaultValueAvailable() would understandably return true however isOptional() would return false as the next parameter ($bar) has no default value and is therefore not optional. To support the non-optional $bar parameter, $foo must itself be non-optional.

Hope this makes sense ;)

I've noted that behaviour differs across PHP versions. 5.5 returns the above whereas 5.4 says parameter 1 is both not optional and has no default value.

  • PHP 5.4 - https://eval.in/154641
  • PHP 5.5 - https://eval.in/154642


回答2:

The function isDefaultValueAvailable can work only on user defined function, and not work on system functions (PHP core).

So, as example:

class Foo
{
    public function foo($var = null)
    {
    }
}

// Get the "var" argument in method Foo::foo
$refParameter = (new ReflectionClass('Foo'))->getMethod('foo')->getParameters()[0];

print "User function Foo::foo:\n\n";

print 'Optional: ' . ($refParameter->isOptional() ? 'true' : 'false') . "\n";
print 'Default available: ' . ($refParameter->isDefaultValueAvailable() ? 'true' : 'false') . "\n";
if ($refParameter->isDefaultValueAvailable()) {
    print 'Default value: ' . var_export($refParameter->getDefaultValue(), 1);
}

print "\n\n";

print "System function substr\n\n";

// Get the "length" parameter from function substr
$refParameter = (new \ReflectionFunction('substr'))->getParameters()[2];

print 'Optional: ' . ($refParameter->isOptional() ? 'true' : 'false') . "\n";
print 'Default available: ' . ($refParameter->isDefaultValueAvailable() ? 'true' : 'false') . "\n";
if ($refParameter->isDefaultValueAvailable()) {
    print 'Default value: ' . var_export($refParameter->getDefaultValue(), 1);
}

print "\n\n";

And, this code shows: your can get default value only from user defined function and can not get from system function (substr as example). But the method isOptional returned true in user defined function and system function.

Conclusion:

  • If your want check the parameter is optional, your must use isOptional method.
  • You can get the default value only from the user defined function.
  • You can not use method isDefaultValueAvailable on system (PHP) defined function.

Source: https://github.com/php/php-src/blob/ccf863c8ce7e746948fb060d515960492c41ed27/ext/reflection/php_reflection.c#L2536-L2573