PHP Function Reference

PHP get_object_vars() Function



The PHP get_object_vars() function is used to get the accessible non-static properties of the given object according to scope.

Syntax

get_object_vars(object)

Parameters

object Required. Specify an object instance.

Return Value

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

Example: get_object_vars() example

The example below shows the usage of get_object_vars() function.

<?php
class myClass {
  private $a;
  public $b = 1;
  public $c;
  private $d;
  static $e;

  //class method
  public function test() {
    print_r(get_object_vars($this));
  }
}

$myObj = new myClass();

//external call
print_r(get_object_vars($myObj));

//internal call
$myObj->test();
?>

The output of the above code will be:

Array
(
    [b] => 1
    [c] => 
)
Array
(
    [a] => 
    [b] => 1
    [c] => 
    [d] => 
)

Example: using with derived class object

Consider the example below where this function is used with a derived class object.

<?php
class myClass {
  private $a;
  public $b = 1;
  public $c;
  private $d;
  static $e;

  //class method
  public function test() {
    print_r(get_object_vars($this));
  }
}

class newClass extends myClass {
  public $f = 2;
  private $g;
  static $h;
}

$myObj = new newClass();

//external call
print_r(get_object_vars($myObj));

//internal call
$myObj->test();
?>

The output of the above code will be:

Array
(
    [f] => 2
    [b] => 1
    [c] => 
)
Array
(
    [f] => 2
    [a] => 
    [b] => 1
    [c] => 
    [d] => 
)

❮ PHP Classes/Objects Reference

5