Skip to content

Instantly share code, notes, and snippets.

@blackandred
Created July 30, 2018 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackandred/3550e5abfc742c67d451ceb50a1df007 to your computer and use it in GitHub Desktop.
Save blackandred/3550e5abfc742c67d451ceb50a1df007 to your computer and use it in GitHub Desktop.
PHP: Get all implemented traits recursively in parents, parents-parents, parents-...-parents
<?php declare(strict_types=1);
class SomeThing
{
private function getClassTraitNames(string $className = null): array
{
if ($className === null || !class_exists($className)) {
return [];
}
$ref = new ReflectionClass($className);
$traits = [$ref->getTraitNames()];
while ($ref) {
$traits[] = $ref->getTraitNames();
$ref = $ref->getParentClass();
if (!$ref) {
break;
}
}
return array_merge(...$traits);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment