Bug #75809 [Opn->Dup]: self type-hint in anonymous function in trait is wrong on successive calls
| From: | pmmaga@php.net | Date: | Fri, 12 Jan 2018 11:17:24 +0000 |
| Subject: | Bug #75809 [Opn->Dup]: self type-hint in anonymous function in trait is wrong on successive calls | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-213492@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75809&edit=1
ID: 75809
Updated by: pmmaga@php.net
Reported by: vanleeuwen dot jarno at gmail dot com
Summary: self type-hint in anonymous function in trait is
wrong on successive calls
-Status: Open
+Status: Duplicate
Type: Bug
Package: Class/Object related
Operating System: Any
PHP Version: 7.0.27
Block user comment: N
Private report: N
New Comment:
Thanks for the confirmation.
Previous Comments:
------------------------------------------------------------------------
[2018-01-12 11:12:19] vanleeuwen dot jarno at gmail dot com
Yes, I can confirm this is the same bug. Sorry for the duplicate bug report.
------------------------------------------------------------------------
[2018-01-12 11:07:48] pmmaga@php.net
Can you confirm that this is a duplicate of https://bugs.php.net/bug.php?id=75079 ?
------------------------------------------------------------------------
[2018-01-12 10:53:00] vanleeuwen dot jarno at gmail dot com
Description:
------------
Starting from PHP 7.0, the self type-hint in an anonymous function within a trait refers back to the
first class that created/used the anonymous function. The bug occurs in PHP 7.0, 7.1 and 7.2. This
was no problem in PHP 5.6 (https://3v4l.org/oKeSG).
In the example below, classes Foo and Bar do not share anything except for the trait. The anonymous
function is created within the local scope of a non-static method, so should not be shared among
different objects implementing this trait.
Test script:
---------------
<?php
trait Feature {
public function getClass() {
$func = function (self $instance) {
print get_class($instance);
};
$func($this);
}
}
class Foo {
use Feature;
}
class Bar {
use Feature;
}
(new Foo)->getClass();
(new Bar)->getClass();
Expected result:
----------------
FooBar
Actual result:
--------------
Foo
Fatal error: Uncaught TypeError: Argument 1 passed to Bar::{closure}() must be an instance of Foo,
instance of Bar given, called in /in/EafpA on line 24 and defined in /in/EafpA:17
Stack trace:
#0 /in/EafpA(24): Bar->{closure}(Object(Bar))
#1 /in/EafpA(54): Bar->execute()
#2 {main}
thrown in /in/EafpA on line 17
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75809&edit=1