get_object_vars

(PHP 4 , PHP 5)

get_object_vars -- Retorna uma matriz associativa com as propriedades do objeto

Descrição

array get_object_vars ( object obj )

Esta função retorna uma matriz associativa com as propriedades definidas do objeto informado em obj.

Nota: Em versões anteriores ao PHP 4.2.0, se as variáveis declaradas na classe de qual obj é uma instância e que não tenham valores assimilados, esses não serão retornados na matriz. Apartir do PHP 4.2.0, ela terá o valor NULL.

Exemplo 1. Uso de get_object_vars()

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

?>

A saída do programa acima será:

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

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

Veja também get_class_methods() e get_class_vars().