13

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;
}
3
  • 1
    Be very careful with those nested ternary statements. Nothing but trouble IMHO Commented May 21, 2014 at 5:51
  • You have any example it fails? ternary statements? Commented May 21, 2014 at 5:52
  • 2
    They're just hard to read and the logic process is not always clear. See the note here ~ php.net/manual/language.operators.comparison.php#example-137 Commented May 21, 2014 at 5:54

2 Answers 2

12

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.

Demo: https://3v4l.org/qgbh7

Sign up to request clarification or add additional context in comments.

2 Comments

You mean, in your example, $foo='foo' will make sense if i call it like this foo(null,'hello') but not foo('helllo')
@LNT Well, that would just set $foo to null. It's a silly way to write a function but not impossible, therefore the reflection utils need to differentiate
6

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

1 Comment

Thanks, got your point, I m only using it for user defined fucntions

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.