Saturday, August 30, 2025
HomeLanguagesPHP class_uses() Function

PHP class_uses() Function

The class_uses() function is an inbuilt function in PHP where the traits utilized by the given class, will be returned.

Syntax:

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

Parameters: This function accepts the two parameters that are described below

  • object_or_class: The name of the class or object passed in this parameter.
  • autoload: This is an optional parameter for whether or not to automatically load the class file if it has not been loaded yet. The default is true.

Return value: If the function is successfully executed it will return an array otherwise it will return “false”.

Example 1: The following code demonstrates the class_uses() function.

PHP




<?php
  
trait MyTrait {
    public function hello() {
        echo "Hello, world!\n";
    }
}
  
class MyClass {
    use MyTrait;
}
  
$traits = class_uses("MyClass");
  
echo "The following traits are used by MyClass:\n";
foreach ($traits as $trait) {
    echo "- $trait\n";
}
  
?>


Output:

The following traits are used by MyClass:
- MyTrait

Example 2: The following code demonstrates the class_uses() function.

PHP




<?php
  
trait MyTrait1 {
    public function hello() {
        echo "Hello, world from Trait 1!\n";
    }
}
  
trait MyTrait2 {
    public function goodbye() {
        echo "Goodbye, world from Trait 2!\n";
    }
}
  
class MyClass {
    use MyTrait1, MyTrait2;
}
  
$traits = class_uses("MyClass");
  
echo "The following traits are used by MyClass:\n";
foreach ($traits as $trait) {
    echo "- $trait\n";
}
?>


Output:

The following traits are used by MyClass:
- MyTrait1
- MyTrait2

Reference: https://www.php.net/manual/en/function.class-uses.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6733 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS