get_class_vars()

get_class_vars() — Get the default properties of the class.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

get_class_vars(string $class): array

Parameters

ParameterTypeWhat it takes
$classstringThe class name

Return value

Returns an associative array of declared properties visible from the current scope, with their default value. The resulting array elements are in the form of varname => value. In case of an error, it returns false.

Example

A runnable snippet:

class MyClass { public $var1; // This has no explicit default value (technically it has NULL as a default)... public $var2 = "xyz"; public $var3 = 100; private $var4; public function __construct() { // Change some properties $this->var1 = "foo"; $this->var2 = "bar"; return true; } } $my_class = new MyClass(); $class_vars = get_class_vars(get_class($my_class)); foreach ($class_vars as $name => $value) { echo "{$name}: ", var_export($value, true), "\n"; }

Output:

var1: NULL var2: 'xyz' var3: 100

See also

get_class_methods(), get_object_vars()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page get_class_vars() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.