get_class_vars

Returns a class's declared properties and their default values, without creating an instance.

Signature
get_class_vars(string $class): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$classstringThe name of an existing class.

Return value

arrayReturns an associative array of declared properties visible from the current scope, in the form varname => value, with their default value. Returns false on error.

Errors

Returns false rather than throwing when the class does not exist; passing a non-string throws a TypeError.

Changelog

  • PHP 5.0.3The function began honouring the calling scope. Before this, callers outside the class could see protected and private properties.

get_class_vars() reads the property declarations of a class and returns each name mapped to the default value written in the source. Because it works on a class name, no constructor runs and no object is created — this is the state a fresh instance would start from, not the state of any particular object.

When to reach for it

It is a quick way to read configuration defaults declared as properties, to build a whitelist of assignable fields for a mass-assignment guard, or to diff a live object's values against the class defaults so you only persist what changed.

Things that trip people up

  • Like get_class_methods(), it respects the calling scope. Called from outside the class you get public properties only, so the array can look surprisingly short from a helper or a test.
  • Unlike get_object_vars(), it includes static properties, since a static property is a class property.
  • It reports declarations, so dynamic properties added at runtime never appear.
  • Typed properties declared without a default have no value to report and are treated as uninitialized rather than null.

Return the class name from a variable and forget it exists, and you get false, not an exception — check the result before iterating.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Read a settings class's declared defaults without instantiating it
  • Build a whitelist of fillable property names for a mass-assignment guard
  • Diff an object's current values against the class defaults before saving

Frequently asked questions

What is the difference between get_class_vars and get_object_vars?

get_class_vars() takes a class name and returns declared properties with their default values, including static ones. get_object_vars() takes an instance and returns its current non-static values, including dynamic properties.

Does get_class_vars show private properties?

Only when you call it from inside the class. From outside the class it returns public properties only, matching normal visibility rules.

What does get_class_vars return for a typed property with no default?

Nothing — the property is uninitialized rather than null, so it is not present in the array. Use property_exists() if you need to know the property is declared.

More examples

Scope decides what you see

The same function call returns one key from the outside and three from inside the class. If a debug helper reports fewer properties than you expect, the calling scope is almost always the reason.

Example · php
<?php
class Door {
    public string $colour = 'red';
    protected int $width = 90;
    private bool $locked = true;

    public static function insideView(): array { return get_class_vars(self::class); }
}

print_r(array_keys(get_class_vars('Door')));  // outside: public only
print_r(array_keys(Door::insideView()));      // inside: everything

Using defaults as an assignment whitelist

array_intersect_key() drops isAdmin because it is not a declared property, so the injected flag never reaches the object. The output shows only name and age were assigned.

Example · php
<?php
class Profile {
    public string $name = '';
    public int $age = 0;
}

$input = ['name' => 'Ada', 'age' => 36, 'isAdmin' => true];
$allowed = get_class_vars('Profile');

$p = new Profile();
foreach (array_intersect_key($input, $allowed) as $k => $v) {
    $p->$k = $v;
}
print_r(get_object_vars($p));

Discussion

  • Be the first to comment on this lesson.