|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-08-11 15:24 UTC] laruence@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 07:00:01 2025 UTC |
Description: ------------ When calling ReflectionParameter::isDefaultValueAvailable() on Closure::__invoke() as an object method, it returns the incorrect value. However, when calling it on a closure as a function, it returns the correct value. Examples below. Test script: --------------- <?php /** * the expected behavior from an object */ class Foo { public function __invoke($baz = 'qux') { return $baz; } } $object = new Foo; var_dump(is_object($object)); // true, so PHP thinks it's an object $method = new ReflectionMethod($object, '__invoke'); // object method exists $params = $method->getParameters(); var_dump($params[0]->isDefaultValueAvailable()); // true /** * now test a closure */ // note the default value $closure = function ($baz = 'qux') { return $baz; }; // treat it as an object: available = false (unexpected) var_dump(is_object($closure)); // true, so PHP thinks it's an object $method = new ReflectionMethod($closure, '__invoke'); // object method exists $params = $method->getParameters(); var_dump($params[0]->isDefaultValueAvailable()); // false // treat it as a function: available = true (expected) $func = new ReflectionFunction($closure); $params = $func->getParameters(); var_dump($params[0]->isDefaultValueAvailable()); // true Expected result: ---------------- I figured, since PHP seems to think the Closure is an object, that one could get a correct isDefaultValueAvailable() from reflecting on __invoke() as an object method (in this case, boolean TRUE). Actual result: -------------- It returns boolean FALSE when reflecting on Closure::__invoke() as an object method.