Laravel 5: Finding What Traits a Class Uses With class_uses_recursive

April 15, 2018 —John Koster

The class_uses_recursive function is functional similar to the trait_uses_recursive function. It will return an array of all the traits that are used by the given class, its traits and any base classes.

#Signature

The signature of the class_uses_recursive function is:

1function class_uses_recursive(
2 $class
3);

#Example Use

Using the ExampleClass from the previous example:

1$traits = class_uses_recursive('ExampleClass');

the $traits array would contain the following values:

1array(4) {
2 ["TraitTwo"] "TraitTwo"
3 ["TraitThree"] "TraitThree"
4 ["TraitOne"] "TraitOne"
5 ["TraitFour"] "TraitFour"
6}

it should be noted that the above values contain TraitFour which is used by ExampleClass's base class. Additionally, the class_uses_recursive function will not work on class instances. If a class instance is supplied an ErrorException will be thrown.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.