Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

class_uses

Description

The class_uses of SPL for PHP returns the traits used by the given class.

Syntax

class_uses(
    object|string $object_or_class,
    bool $autoload = true
): array|false

Parameters

object_or_class

An object (class instance) or a string (class name).

autoload

Whether to autoload if not already loaded.

Return

Returns an array on success, or false when the given class doesn't exist.

Examples

1 · object_or_class · object

<?

trait mytrait
{
}
class myclass
{
    use mytrait;
}

$object_or_class = new myclass;

$return = class_uses($object_or_class);

print_r($return);

?>
Array
(
    [mytrait] => mytrait
)

2 · object_or_class · class

<?

trait mytrait
{
}
class myclass
{
    use mytrait;
}

$object_or_class = "myclass";

$return = class_uses($object_or_class);

print_r($return);

?>
Array
(
    [mytrait] => mytrait
)

3 · autoload · false

<?

spl_autoload_register();

trait mytrait
{
}
class myclass
{
    use mytrait;
}

$object_or_class = "myclass";
$autoload = false;

$return = class_uses($object_or_class, $autoload);

print_r($return);

?>
Array
(
    [mytrait] => mytrait
)

4 · autoload · true

<?

spl_autoload_register();

trait mytrait
{
}
class myclass
{
    use mytrait;
}

$object_or_class = "myclass";
$autoload = true;

$return = class_uses($object_or_class, $autoload);

print_r($return);

?>
Array
(
    [mytrait] => mytrait
)

5 · multiple

<?

trait mytrait1
{
}
trait mytrait2
{
}
trait mytrait3
{
}
class myclass
{
    use mytrait1;
    use mytrait2;
    use mytrait3;
}

$object_or_class = "myclass";

$return = class_uses($object_or_class);

print_r($return);

?>
Array
(
    [mytrait1] => mytrait1
    [mytrait2] => mytrait2
    [mytrait3] => mytrait3
)
HomeMenu