The PHP get_called_class API performs a focused operation on an object or value. The examples below show its effect on actual data.
Returns the late-static-binding class name from a method called in class scope.
What Is get_called_class in PHP?
Returns the late-static-binding class name from a method called in class scope.
Syntax:
get_called_class(): string
This operation has no parameters.
Return type declared by PHP: string. 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
class BaseReporter { public static function reveal(): string { return get_called_class(); } }
class ChildReporter extends BaseReporter {}
echo ChildReporter::reveal();
Output:
ChildReporter
Returns the late-static-binding class name from a method called in class scope.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class BaseReporter { public static function reveal(): string { return get_called_class(); } }
class ChildReporter extends BaseReporter {}
echo ChildReporter::reveal();
// The output reflects definitions in this PHP process.
Output:
ChildReporter
Calling this outside an allowed class context raises an Error.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
class BaseReporter { public static function reveal(): string { return get_called_class(); } }
class ChildReporter extends BaseReporter {}
echo ChildReporter::reveal();
// Use this result while deciding how to handle optional application features.
Output:
ChildReporter
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Calling this outside an allowed class context raises an Error.
- Returns the late-static-binding class name from a method called in class scope.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
get_called_class | Returns the late-static-binding class name from a method called in class scope. |
| get_class() | get_called_class() uses late static binding in a class context; get_class() reads the runtime class of an object. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of get_called_class | Calling this outside an allowed class context raises an Error. |
| 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 | get_called_class() uses late static binding in a class context; get_class() reads the runtime class of an object. |
Key Takeaways
- Returns the late-static-binding class name from a method called in class scope.
- Calling this outside an allowed class context raises an Error.
- Use the example that matches the values, runtime version and dependencies in your application.
get_called_class 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.