<?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();
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
FooBar
Output for 7.1.0 - 7.1.13, 7.2.0 - 7.2.1
Foo
Fatal error: Uncaught TypeError: Argument 1 passed to Bar::{closure}() must be an instance of Foo, instance of Bar given, called in /in/oKeSG on line 10 and defined in /in/oKeSG:6
Stack trace:
#0 /in/oKeSG(10): Bar->{closure}(Object(Bar))
#1 /in/oKeSG(26): Bar->getClass()
#2 {main}
thrown in /in/oKeSG on line 6
Process exited with code 255.