php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65432 ReflectionParameter::isDefaultValueAvailable inconsistent for Closure
Submitted: 2013-08-09 22:57 UTC Modified: 2013-08-11 15:24 UTC
From: pmjones88 at gmail dot com Assigned:
Status: Open Package: Reflection related
PHP Version: 5.4.17 OS: Mac OS X 10.8.4
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-08-09 22:57 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-11 15:24 UTC] laruence@php.net
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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 18:01:24 2019 UTC