Saturday, November 22, 2025
HomeLanguagesPHP | get_object_vars() Function

PHP | get_object_vars() Function

The get_object_vars() function is an inbuilt function in PHP that is used to get the properties of the given object. When an object is made, it has some properties. An associative array of properties of the mentioned object is returned by the function. But if there is no property of the object, then it returns NULL.

Syntax: 

get_object_vars( $object )

Parameters: This function accepts a single parameter as mentioned above and described below: 
$object: This parameter holds the object of an instance.
Return Value: This method returns an associative array object accessible non-static properties for the specified object in scope.

Below programs illustrate the get_object_vars() function in PHP:

Program 1: 

PHP




<?php
 
// Declare a class
class gfg {
     
    // Properties of an object
    // of this class
    private $neveropen = 0.02;
    public $for = 1;
    public $Geeks = "php";
    private $GEEKS;
    static $e;
     
    public function example() {
        var_dump(get_object_vars($this));
    }
}
 
// Create an object of a class
$example = new gfg;
 
// Display properties of the
// newly created object
var_dump(get_object_vars($example));
  
$example->example();
  
?>


Output: 

array(2) {
  ["for"]=>
  int(1)
  ["Geeks"]=>
  string(3) "php"
}
array(4) {
  ["neveropen"]=>
  float(0.02)
  ["for"]=>
  int(1)
  ["Geeks"]=>
  string(3) "php"
  ["GEEKS"]=>
  NULL
}

 

Program 2: 

PHP




<?php
 
// Create a class
   class coordinate {
        
        // The properties of the
        // object of this class
        var $x;
        var $y;
        var $z;
        var $labels;
  
        function coordinate($x, $y, $z) {
            $this->x = $x;
            $this->y = $y;
            $this->z = $z;
        }
  
        function to_set($labels) {
            $this->labels = $labels;
        }
    }
    
    $point1 = new coordinate(0.1, 0.2, 0.3);
    print_r(get_object_vars($point1));
  
    $point1->to_set("point 1");
    print_r(get_object_vars($point1));
 
?>


Output: 

Array
(
    [x] => 0.1
    [y] => 0.2
    [z] => 0.3
    [labels] => 
)
Array
(
    [x] => 0.1
    [y] => 0.2
    [z] => 0.3
    [labels] => point 1
)

 

Reference: https://www.php.net/manual/en/function.get-object-vars.php
 

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11929 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS