The PHP get_class_vars API performs a focused operation on an object or value. The examples below show its effect on actual data.
Returns default properties visible from the calling scope.
What Is get_class_vars in PHP?
Returns default properties visible from the calling scope.
Syntax:
get_class_vars(string $class): array
The method accepts the following parameters:
$class— Required. The class name or object to inspect.
Return type declared by PHP: array. 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 EnvironmentConfig { public string $stage = "test"; private int $port = 8080; }
print_r(get_class_vars(EnvironmentConfig::class));
Output:
Array
(
[stage] => test
)
Returns default properties visible from the calling scope.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class EnvironmentConfig { public string $stage = "test"; private int $port = 8080; }
print_r(get_class_vars(EnvironmentConfig::class));
// The output reflects definitions in this PHP process.
Output:
Array
(
[stage] => test
)
Uninitialized typed properties and private visibility require careful handling.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
class EnvironmentConfig { public string $stage = "test"; private int $port = 8080; }
print_r(get_class_vars(EnvironmentConfig::class));
// Use this result while deciding how to handle optional application features.
Output:
Array
(
[stage] => test
)
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Uninitialized typed properties and private visibility require careful handling.
- Returns default properties visible from the calling scope.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
get_class_vars | Returns default properties visible from the calling scope. |
| get_object_vars() | get_class_vars() reports declared defaults; get_object_vars() reads initialized visible instance properties. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of get_class_vars | Uninitialized typed properties and private visibility require careful handling. |
| 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_class_vars() reports declared defaults; get_object_vars() reads initialized visible instance properties. |
Key Takeaways
- Returns default properties visible from the calling scope.
- Uninitialized typed properties and private visibility require careful handling.
- Use the example that matches the values, runtime version and dependencies in your application.
get_class_vars 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.