3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait A1 { public $propA1; } trait A2 { use A1; public $propA2; } trait A3 { use A2; public $propA3; } trait B1 { public $propB1; } trait B2 { use B1; public $propB2; } trait B3 { use B2; public $propB3; } class X { use A3; } class Y extends X { use B3; } function getTraits(ReflectionClass $c): array { $t = array_values(array_map(getTraits(...), $c->getTraits())); return array_merge($c->getTraitNames(), ...$t); } $rc = new ReflectionClass('Y'); $traits = []; $c = $rc; while ($c) { $traits[] = getTraits($c); $c = $c->getParentClass(); } var_dump(array_merge(...$traits));
Output for 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array(6) { [0]=> string(2) "B3" [1]=> string(2) "B2" [2]=> string(2) "B1" [3]=> string(2) "A3" [4]=> string(2) "A2" [5]=> string(2) "A1" }

preferences:
41.33 ms | 724 KiB | 5 Q