|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2018-09-29 19:11 UTC] nikic@php.net
-Status: Open
+Status: Wont fix
[2018-09-29 19:11 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 20:00:01 2025 UTC |
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