php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67479 ReflectionMethod::getClosure should allow binding to NULL (static context)
Submitted: 2014-06-19 13:56 UTC Modified: 2018-09-29 19:11 UTC
From: baileyp at comast dot net Assigned:
Status: Wont fix Package: Reflection related
PHP Version: 5.5.13 OS: Windows/Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: baileyp at comast dot net
New email:
PHP Version: OS:

 

 [2014-06-19 13:56 UTC] baileyp at comast dot net
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-29 19:11 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2018-09-29 19:11 UTC] nikic@php.net
The ability to call a non-static method with $this==null has been deprecated in PHP 7 and will go away in PHP 8. The fact that closure rebinding allowed this without warning was an oversight, which I've rectified in https://github.com/php/php-src/commit/fc18f44213d59190a3e524e5c2f1a33f73be2cda for PHP 7.4.

As this behavior is on its way out, it doesn't really make sense to add extra support for it in ReflectionMethod::getClosure(), so I'm going to close this issue as Won't Fix.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 13:01:30 2024 UTC