Req #67479 [Opn->Wfx]: ReflectionMethod::getClosure should allow binding to NULL (static context)

From: Date: Sat, 29 Sep 2018 19:11:06 +0000
Subject: Req #67479 [Opn->Wfx]: ReflectionMethod::getClosure should allow binding to NULL (static context)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217293@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67479&edit=1

 ID:                 67479
 Updated by:         nikic@php.net
 Reported by:        baileyp at comast dot net
 Summary:            ReflectionMethod::getClosure should allow binding to
                     NULL (static context)
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Reflection related
 Operating System:   Windows/Linux
 PHP Version:        5.5.13
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2014-06-19 13:56:47] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=67479&edit=1


Thread (2 messages)

« previous php.bugs (#217293) next »