Bug #75809 [NEW]: self type-hint in anonymous function in trait is wrong on successive calls
From: vanleeuwen dot jarno at gmail dot com
Operating system: Any
PHP version: 7.0.27
Package: Class/Object related
Bug Type: Bug
Bug description:self type-hint in anonymous function in trait is wrong on successive calls
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 bug report at https://bugs.php.net/bug.php?id=75809&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75809&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75809&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75809&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75809&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75809&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75809&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75809&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75809&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75809&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75809&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75809&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75809&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75809&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75809&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75809&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75809&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75809&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75809&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75809&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75809&r=mysqlcfg
Thread (4 messages)
- vanleeuwen dot jarno at gmail dot com