Trait
composer require php-standard-library/trait
The Trait component provides type-safe wrappers around PHP's built-in trait reflection and existence checks. It replaces loose trait_exists() calls with a consistent, intention-revealing API.
Usage
PSL separates existence checks into two distinct functions -- exists() (triggers autoloading) and defined() (checks only already-loaded definitions) -- so the intent is always clear.
use Psl\Interface;
use Psl\IO;
use Psl\Trait;
IO\write_line('JsonSerializable exists: %s', Interface\exists(JsonSerializable::class) ? 'yes' : 'no');
IO\write_line('JsonSerializable defined: %s', Interface\defined(JsonSerializable::class) ? 'yes' : 'no');
// Check a non-existent trait
IO\write_line('NonExistentTrait exists: %s', Trait\exists('NonExistentTrait') ? 'yes' : 'no');
See src/Psl/Trait/ for the full API.