Skip to content
Related Articles

Related Articles

Improve Article
PHP | get_object_vars() Function
  • Last Updated : 18 Jun, 2020

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:




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




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




My Personal Notes arrow_drop_up
Recommended Articles
Page :