Wednesday, September 3, 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
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS