get_declared_traits
Returns the names of every trait currently defined in the running script.
get_declared_traits(): array- Added in PHP 5.4
Return value
arrayReturns an array with names of all declared traits in values.
get_declared_traits() lists the traits the engine currently knows about. Traits arrived in PHP 5.4, and unlike classes and interfaces there are essentially no built-in ones, so on a plain script the array contains only what your code (and your dependencies) declared. That makes it the cleanest of the three get_declared_* functions to read.
When to reach for it
Debugging and tooling. A trait that appears twice under near-identical names, or one you expected and cannot find, usually explains an autoloading or namespace mistake faster than reading files. It is also handy in a static-analysis-style script that checks a codebase's trait inventory after including a bootstrap.
Things that trip people up
- Same rule as its siblings: it reflects what has been loaded, not what exists on disk. Autoloaded traits appear only once something has used them.
- A trait's name never appears in
get_declared_classes(), andclass_exists()on a trait returnsfalse— usetrait_exists(). - To answer "which traits does this class use?", reach for
class_uses(). That function does not walk parent classes, so combine it withclass_parents()for the full picture. - The declared list says nothing about whether a trait is used anywhere.
Example
When to use it
- Confirm during boot that an expected trait was actually loaded rather than silently missing
- Audit a codebase's trait inventory in a tooling script after including the bootstrap
- Debug a duplicate or misnamespaced trait that an autoloader is resolving to the wrong file
Frequently asked questions
How do I check whether one specific trait exists?
Use trait_exists($name), which also triggers the autoloader. Scanning get_declared_traits() only sees traits that are already loaded.
Why is my trait not in get_declared_traits?
Almost always because nothing has loaded it yet. Traits are autoloaded like classes, so the name only appears after a class uses it or trait_exists() pulls it in.
How do I list the traits a class uses?
Call class_uses($objectOrClass). It returns only the traits declared directly on that class, so walk class_parents() as well if you need inherited ones.
More examples
Which traits does a class actually use?
class_uses('Post') returns only Softdeletes because it does not look at parents — a classic source of "my trait check keeps failing" bugs. get_declared_traits() shows both traits exist regardless of who uses them.
<?php
trait Sluggable {}
trait Softdeletes {}
class BaseModel { use Sluggable; }
class Post extends BaseModel { use Softdeletes; }
print_r(array_values(class_uses('Post'))); // own traits only
print_r(array_values(class_uses(get_parent_class('Post')))); // parent's traits
print_r(get_declared_traits()); // everything loadedDetecting an unused trait
Cross-referencing the trait list against every declared class's class_uses() finds traits nothing consumes. Auditable is reported as unused, which is the kind of check a codebase cleanup script performs.
<?php
trait Cacheable {}
trait Auditable {}
class Report { use Cacheable; }
$used = [];
foreach (get_declared_classes() as $c) {
$used = array_merge($used, class_uses($c) ?: []);
}
foreach (get_declared_traits() as $t) {
echo $t, isset($used[$t]) ? " (used)\n" : " (declared but unused)\n";
}
Discussion