Understanding get_class_vars() in PHP: A Deep Dive
When working with classes in PHP, you may often need to retrieve the default values of a class's properties. PHP provides the get_class_vars()
function to make this task easier. However, this function comes with some specific behaviors and limitations that you should be aware of.
What is get_class_vars()?
The get_class_vars()
function is used to retrieve an associative array of class properties along with their default values. This function does not require an instance of the class—it works directly on the class definition.
Syntax
array get_class_vars(string $class_name)
$class_name
: The name of the class whose properties you want to fetch.- Returns: An associative array where keys are property names and values are their default values.
Key Characteristics and Limitations
- Only public and protected properties are returned.
- Private properties are NOT included, even if they have default values.
- Static properties are included.
- Only properties with explicitly defined default values are returned. Properties that do not have a default value will appear with an empty value.
- Dynamically assigned properties at runtime are NOT included.
Example Usage
Basic Example
class Demo {
public string $name = "John";
protected int $age = 25;
private string $secret = "hidden";
public static string $status = "active";
}
$vars = get_class_vars(Demo::class);
print_r($vars);
Expected Output
Array
(
[name] => John
[age] => 25
[status] => active
)
Explanation:
name
(public) is included.age
(protected) is included.status
(static public) is included.secret
(private) is NOT included.
Handling Properties Without Default Values
If a class property is declared but does not have an assigned default value, get_class_vars()
will return it with an empty value.
class Test {
public string $name;
protected int $age;
public static string $status;
}
print_r(get_class_vars(Test::class));
Output:
Array
(
[name] =>
[age] =>
[status] =>
)
Even though the properties are defined, they appear as empty because no default values were explicitly set.
What About Dynamically Assigned Properties?
Properties that are dynamically assigned after object creation are not included in get_class_vars()
because the function only retrieves the values defined in the class itself.
class Example {
public string $name = "Alice";
}
$obj = new Example();
$obj->name = "Bob";
$obj->newProperty = "Dynamically added";
print_r(get_class_vars(Example::class));
Output:
Array
(
[name] => Alice
)
Explanation:
- The function retrieves only the default class properties.
- The dynamically assigned
newProperty
is not included. - Even though
$obj->name
was changed to "Bob",get_class_vars()
still returns the original default value "Alice".
Other Considerations
1. Does Not Work on Object Instances
Unlike get_object_vars()
, which works on instances, get_class_vars()
works only on class definitions.
2. Works with Inheritance
If a class extends another class, get_class_vars()
will not include private properties from the parent class, but it will include protected and public properties.
Example with Inheritance
class ParentClass {
public string $parentProp = "Hello";
protected int $protectedProp = 123;
private string $privateProp = "Secret";
}
class ChildClass extends ParentClass {
public string $childProp = "World";
}
print_r(get_class_vars(ChildClass::class));
Output:
Array
(
[parentProp] => Hello
[protectedProp] => 123
[childProp] => World
)
Explanation:
parentProp
(public) is included.protectedProp
(protected) is included.privateProp
(private) is NOT included.childProp
(public) is included.
Use Cases for get_class_vars()
- Reflection and Debugging – Helps in inspecting class structures without creating an instance.
- Building Frameworks or Libraries – Useful for meta-programming where you need to analyze classes dynamically.
- Generating Default Configuration Files – If you store configuration settings as class properties,
get_class_vars()
can extract them easily.
Finally
The get_class_vars()
function is a powerful tool for retrieving default class properties, but it comes with important limitations such as excluding private properties and ignoring runtime assignments. When used correctly, it can be highly effective in debugging, meta-programming, and class analysis.
If you need to get instance properties, consider using get_object_vars()
instead. Understanding the differences will help you decide when and where to use get_class_vars()
effectively in your PHP projects!
Comments ()