3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait x { public function bar() { echo 'Trait x bar()'; } public function foo() { echo 'Trait x foo()'; } } class z { use x; public function foo() { echo 'Class foo()'; } } function getClassAndTraitMethods(ReflectionClass $class) { $traitMethods = []; foreach($class->getTraits() as $trait) { foreach($trait->getMethods() as $method) { $traitMethods[] = $method; } } $traitRefs = []; foreach($traitMethods as $traitMethod) { $traitRefs[spl_object_hash($traitMethod)]=$traitMethod->name; } $classMethods = []; $classRefs = []; foreach($class->getMethods() as $classMethod) { $classMethods[]=$classMethod; $classRefs[spl_object_hash($classMethod)] = $classMethod->name; } return ['traitRefs'=>$traitRefs,'classRefs'=>$classRefs]; } $obj = new z; $ref = new ReflectionClass($obj); extract(getClassAndTraitMethods($ref)); $traitOnlyHashes = array_diff( array_keys($traitRefs), array_keys($classRefs) ); $traitOnlyMethods = array_intersect_key($traitRefs, array_flip($traitOnlyHashes)); print_r($traitRefs); print_r($classRefs); print_r($traitOnlyMethods);

preferences:
59.99 ms | 410 KiB | 5 Q