338 of 410 menu

The get_declared_traits Function

The get_declared_traits function returns an array with the names of all traits that have been declared in the current script. This function does not take any parameters and can be useful for debugging or dynamic work with traits.

Syntax

get_declared_traits();

Example

Get a list of all declared traits in the script:

<?php trait Trait1 {} trait Trait2 {} $res = get_declared_traits(); print_r($res); ?>

Code execution result:

['Trait1', 'Trait2']

Example

Check the result of calling the function without declared traits:

<?php $res = get_declared_traits(); print_r($res); ?>

Code execution result:

[]

Example

Compare the result before and after declaring a trait:

<?php $res1 = get_declared_traits(); trait NewTrait {} $res2 = get_declared_traits(); print_r($res1); print_r($res2); ?>

Code execution result:

[] ['NewTrait']

See Also

byenru