Created
July 30, 2018 06:38
-
-
Save blackandred/3550e5abfc742c67d451ceb50a1df007 to your computer and use it in GitHub Desktop.
PHP: Get all implemented traits recursively in parents, parents-parents, parents-...-parents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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