Monday 26 August 2013

PHP Function get_object_vars()

Syntax

get_object_vars ( $object);

Definition and Usage

This function gets the properties of the given object.

Paramters

ParameterDescription
objectAn object instance.

Return Value

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

Following is the usage of this function:
<?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);
    }
}

// "$label" is declared but not defined
$p1 = new Point2D(1.233, 3.445);
print_r(get_object_vars($p1));

$p1->setLabel("point #1");
print_r(get_object_vars($p1));
?> 
It will produce following result:
Array
 (
     [x] => 1.233
     [y] => 3.445
     [label] =>
 )

 Array
 (
     [x] => 1.233
     [y] => 3.445
     [label] => point #1
 )

No comments:

Post a Comment