3v4l.org

run code in 300+ PHP versions simultaneously
<?php class demo { public function MethodA() { return 'method A from class'; } public function MethodC() { return 'method C from class'; } public function MethodE() { return 'method E from class'; } use tGeneric; } trait tGeneric { public function MethodA() { return 'method A from trait'; } public function MethodB() { return 'method B from trait'; } public function MethodD() { return 'method D from trait'; } public function MethodE() { return 'method E from trait'; } } /** * Given a class name, retrieves the corresponding class' methods that override * trait methods. * * @param string $class_name * @return \ReflectionMethod[] * @throws \ReflectionException */ function getMethodsOverriddenFromTraits(string $class_name): array { $class = new \ReflectionClass($class_name); // Retrieve trait methods $trait_methods = []; foreach ($class->getTraits() as $trait) { foreach ($trait->getMethods() as $trait_method) { $trait_methods[$trait_method->getName()] = $trait_method; } } // Compute class methods that override them $methods_overridden = []; foreach ($class->getMethods() as $class_method) { if (array_key_exists($class_method->getName(), $trait_methods)) { $trait_method = $trait_methods[$class_method->getName()]; if ($class_method->getFileName() !== $trait_method->getFileName() || $class_method->getStartLine() !== $trait_method->getStartLine()) { $methods_overridden[] = $class_method->getName(); } } } return $methods_overridden; } print_r(getMethodsOverriddenFromTraits('demo'));

preferences:
33.85 ms | 405 KiB | 5 Q