How can I check if a an instance of a class uses a Trait? I can't use instanceof
because the Trait is uninstantiable.
Hack is a super set of PHP (and also a sub set, given some legacy stuff was removed), so most of the native functions can be used.
With that being said, you have the class_uses() function, that does what you want.
Here's a simplified use case:
if (in_array(\Foo\Bar::class, class_uses($object))) {
// Do something if $object is using \Foo\Bar trait
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With