Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hack - how to check that an instance uses a Trait?

Tags:

php

hacklang

How can I check if a an instance of a class uses a Trait? I can't use instanceof because the Trait is uninstantiable.

like image 982
Corey Wu Avatar asked Jan 03 '23 00:01

Corey Wu


1 Answers

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
}
like image 199
Quetzy Garcia Avatar answered Jan 05 '23 14:01

Quetzy Garcia