I 've noticed something I cannot explain to myself. I need a little help to check, if an inherited class uses a specific trait. To make it a little clearer, I use the following code.
trait Foo
{
public function say($what)
{
echo $what;
return $this;
}
}
class A
{
uses Foo;
}
class B extends A
{
}
I 'm aware that I should use the class_uses() method, to find all traits used by a class. Butt this does not work on inherited instances.
$b = (new B())->say('hello');
The above example echoes hello. So the trait is inherited successfully and can be used from class B.
$used = class_uses($b);
var_dump($used);
Surprisingly this outputs an empty array. I expected, that class_uses would give me something like Foo
. But it does not.
$reflection = new ReflectionClass('B');
$uses = $reflection->getTraits();
var_dump($uses);
Same expectation here. But it just outputs an empty array.
Why the used trait from class A can not be seen in the inherited class B? Are there any alternatives to solve this problem?