Bug #65432 [Opn]: ReflectionParameter::isDefaultValueAvailable inconsistent for Closure
| From: | laruence@php.net | Date: | Sun, 11 Aug 2013 15:24:40 +0000 |
| Subject: | Bug #65432 [Opn]: ReflectionParameter::isDefaultValueAvailable inconsistent for Closure | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-180875@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65432&edit=1
ID: 65432
Updated by: laruence@php.net
Reported by: pmjones88 at gmail dot com
Summary: ReflectionParameter::isDefaultValueAvailable
inconsistent for Closure
Status: Open
Type: Bug
Package: Reflection related
Operating System: Mac OS X 10.8.4
PHP Version: 5.4.17
Block user comment: N
Private report: N
New Comment:
it's because, closure use a built-in method Closure::__invoke as a proxy to call
the user defined closure function.
so, actually you were checking Closure::__invoke
Previous Comments:
------------------------------------------------------------------------
[2013-08-09 22:57:39] pmjones88 at gmail dot com
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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65432&edit=1