Req #67479 [NEW]: ReflectionMethod::getClosure should allow binding to NULL (static context)
| From: | baileyp at comast dot net | Date: | Thu, 19 Jun 2014 13:56:47 +0000 |
| Subject: | Req #67479 [NEW]: ReflectionMethod::getClosure should allow binding to NULL (static context) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-186259@lists.php.net to get a copy of this message | ||
From: baileyp at comast dot net
Operating system: Windows/Linux
PHP version: 5.5.13
Package: Reflection related
Bug Type: Feature/Change Request
Bug description:ReflectionMethod::getClosure should allow binding to NULL (static context)
Description:
------------
In the latest versions of PHP 5.4 & 5.5, ReflectionMethod::getClosure()
accepts one or zero arguments depending on the context of the method.
For instance methods, the first argument is required and must be an
instance of an object to which you want the $this context of the closure
to be bound (Passing null raises a warning and returns null for the
closure). However, given that you can call Closure::bindTo() with first
parameter as null to make the closure static, I feel that prohibiting
the first parameter for ReflectionMethod::getClosure() from being null
(although somewhat logical given that the method was originally an
instance method) is unnecessary because the requirement of having a
context can later be overridden by calling Closure::bindTo(null). To
achieve the desired result, this forces you to create two closures, when
only one should be necessary.
Test script:
---------------
<?php
class Foo {
public function bar($a) { return $a . 'bc'; }
}
$foo = new Foo();
$barMethod = (new ReflectionObject($foo))->getMethod('bar');
//$bar = $barMethod->getClosure(null); // Line 9
$bar = $barMethod->getClosure($foo)->bindTo(null); // Line 10
var_dump($bar);
echo $bar('a');
// Demo codepad: http://codepad.viper-7.com/CM87iU
Expected result:
----------------
I expect that both lines 9 & 10 (when run individually) successfully
store a closure with static context into $bar.
Actual result:
--------------
The line 10 does what is expected, but commenting line 10 out and
uncommenting line 9 produces a warning and stores NULL in $bar. The
warning is reproduced below:
Warning: ReflectionMethod::getClosure() expects parameter 1 to be
object, null given in ... on line 9
--
Edit bug report at https://bugs.php.net/bug.php?id=67479&edit=1
--