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
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
40 + 5 = ?
Subscribe to this entry?

 
 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC