get_object_vars

Returns an object's accessible non-static properties as a name-to-value associative array.

Signature
get_object_vars(object $object): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$objectobjectAn object instance.

Return value

arrayReturns an associative array of defined object accessible non-static properties for the specified object in scope.

Errors

Throws a TypeError if object is not an object.

Changelog

  • PHP 8.1.0Uninitialized typed properties are consistently excluded from the result rather than appearing with a null value.

get_object_vars() turns an object into an associative array of its current property values. Names come back clean (no mangling), values are the live ones, and the array includes dynamic properties assigned at runtime as well as declared ones.

The scope rule

Which properties you get depends on where the call happens. From outside the class you see public properties only; from inside a method of the class you see private and protected ones too. This is the single most common source of confusion — a helper that returns get_object_vars($obj) gives a very different array from a method that returns get_object_vars($this).

When to reach for it

It is the standard way to implement JsonSerializable::jsonSerialize(), to build the payload for a database insert, or to diff two objects field by field. Inside the class it is a concise toArray().

Things that trip people up

  • Static properties are never included — they belong to the class, not the instance.
  • Typed properties that were never assigned are uninitialized, not null, and are left out entirely.
  • It is shallow: nested objects come back as objects, not arrays.

Example

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

When to use it

  • Implement toArray() or jsonSerialize() on a value object
  • Build the column/value map for a database insert from an entity
  • Compare two objects property by property in a test assertion

Frequently asked questions

Why does get_object_vars not return private properties?

Because it honours the calling scope. Call it from inside the class with get_object_vars($this) to include private and protected properties, or use get_mangled_object_vars() to get them all regardless of scope.

What is the difference between get_object_vars and casting to an array?

An (array) cast returns every property with mangled NUL-byte keys for private and protected ones. get_object_vars() returns clean names but only the properties visible from where you call it.

Does get_object_vars include static properties?

No. Static properties belong to the class, so use get_class_vars() if you need them.

More examples

A toArray() that respects visibility

Inside jsonSerialize() the private note is present and must be removed by hand. The second line calls the function from outside the class, where the note was never included in the first place — the same call, two different results.

Example · php
<?php
class Order implements JsonSerializable {
    public function __construct(
        public int $id = 7,
        public string $status = 'paid',
        private string $internalNote = 'do not ship',
    ) {}

    public function jsonSerialize(): array {
        $vars = get_object_vars($this);   // includes the private note
        unset($vars['internalNote']);      // so drop it explicitly
        return $vars;
    }
}

echo json_encode(new Order()), "\n";
echo json_encode(get_object_vars(new Order())), "\n";

Dynamic and uninitialized properties

The runtime-added extra shows up, but the declared-yet-uninitialized $empty does not. property_exists() still reports true for it, which is why the two functions must not be used interchangeably.

Example · php
<?php
class Bag {
    public string $filled = 'yes';
    public string $empty;      // typed, never assigned
}

$b = new Bag();
$b->extra = 'added at runtime';

print_r(get_object_vars($b));
var_dump(property_exists($b, 'empty'));
var_dump(array_key_exists('empty', get_object_vars($b)));

Discussion

  • Be the first to comment on this lesson.