get_object_vars() function in PHP


The get_object_var() function gets the properties of the given object. It returns an associative array of defined object properties for the specified object.

Syntax

get_object_vars(object)

Parameters

  • object − An object instance.

Return

The get_object_var() function returns an associative array of defined object properties for the specified object. If a property have not been assigned a value, it will be returned with a NULL value.

Example

The following is an example −

 Live Demo

<?php
   class Point2D {
      var $x, $y;
      var $label;

    function Point2D($x, $y) {
      $this->x = $x;
      $this->y = $y;
   }

    function setLabel($label) {
      $this->label = $label;
    }

    function getPoint() {
      return array("x" => $this->x, "y" => $this->y, "label" => $this->label);
    }
   }
   $p1 = new Point2D(9.675, 8.321);
   print_r(get_object_vars($p1));

   $p1->setLabel("point #1");
   print_r(get_object_vars($p1));

?>

Output

The following is the output −

Array (
   [x] => 9.675
   [y] => 8.321
   [label] =>
)
Array (
   [x] => 9.675
   [y] => 8.321
   [label] => point #1
)

Updated on: 24-Dec-2019

870 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements