The get_class_vars Function
The get_class_vars
function returns an associative array of properties of the specified class.
The array keys are the property names, and the values are their default values.
The function accepts one parameter - the class name as a string.
Syntax
get_class_vars(string $class_name);
Example
Get class properties with their default values:
<?php
class MyClass {
public $var1 = 'value1';
public $var2 = 'value2';
private $var3 = 'value3';
}
$res = get_class_vars('MyClass');
print_r($res);
?>
Code execution result:
['var1' => 'value1', 'var2' => 'value2']
Example
The function returns only public properties:
<?php
class TestClass {
public $publicVar = 1;
protected $protectedVar = 2;
private $privateVar = 3;
}
$res = get_class_vars('TestClass');
print_r($res);
?>
Code execution result:
['publicVar' => 1]
Example
Working with dynamic properties:
<?php
class DynamicClass {
public $defaultVar = 'default';
}
$obj = new DynamicClass();
$obj->dynamicVar = 'dynamic';
$res = get_class_vars('DynamicClass');
print_r($res);
?>
Code execution result:
['defaultVar' => 'default']
See Also
-
the get_object_vars function,
which returns object properties -
the property_exists function,
which checks for property existence -
the get_class_methods function,
which returns class methods