<?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();
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.