PHP | get_object_vars() Function

The get_object_vars() function is an inbuilt function in PHP which 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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare a class
class gfg {
      
    // Properties of an object
    // of this class
    private $geeks = 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;
  
// Displlay properties of the
// newly created object
var_dump(get_object_vars($example));
   
$example->example();
   
?>

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
  
?>

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.