The PHP class_uses API performs a focused operation on an object or value. The examples below show its effect on actual data.
Lists traits used directly by a class.
What Is class_uses in PHP?
Lists traits used directly by a class.
Syntax:
class_uses($object_or_class, bool $autoload = ...): array|false
The method accepts the following parameters:
$object_or_class— Required. A PHP parameter named object_or_class; the examples below show a supported value.$autoload— Optional. A bool parameter named autoload; the examples below show a supported value.
Return type declared by PHP: array|false. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
trait HasIdentifier { public function identifier(): int { return 7; } }
class ItemEntity { use HasIdentifier; }
print_r(class_uses(ItemEntity::class));
Output:
Array
(
[HasIdentifier] => HasIdentifier
)
Lists traits used directly by a class.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
trait HasIdentifier { public function identifier(): int { return 7; } }
class ItemEntity { use HasIdentifier; }
print_r(class_uses(ItemEntity::class));
// The output reflects definitions in this PHP process.
Output:
Array
(
[HasIdentifier] => HasIdentifier
)
Traits used by parent classes are not automatically included in the direct list.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
trait HasIdentifier { public function identifier(): int { return 7; } }
class ItemEntity { use HasIdentifier; }
print_r(class_uses(ItemEntity::class));
// Use this result while deciding how to handle optional application features.
Output:
Array
(
[HasIdentifier] => HasIdentifier
)
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Traits used by parent classes are not automatically included in the direct list.
- Lists traits used directly by a class.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
class_uses | Lists traits used directly by a class. |
| class_implements() | class_uses() reports traits; class_implements() reports interface contracts. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of class_uses | Traits used by parent classes are not automatically included in the direct list. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | class_uses() reports traits; class_implements() reports interface contracts. |
Key Takeaways
- Lists traits used directly by a class.
- Traits used by parent classes are not automatically included in the direct list.
- Use the example that matches the values, runtime version and dependencies in your application.
class_uses is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.