get_object_vars

(PHP 4, PHP 5, PHP 7, PHP 8)

get_object_vars获取指定对象的属性

说明

get_object_vars(object $object): array

根据作用域获取指定 object 的可访问非静态属性。

参数

object

对象实例。

返回值

返回指定 object 在当前作用域的属性组成的关联数组,属性为已定义、非静态、可访问。

范例

示例 #1 get_object_vars() 使用

<?php

class foo {
private
$a;
public
$b = 1;
public
$c;
private
$d;
static
$e;

public function
test() {
var_dump(get_object_vars($this));
}
}

$test = new foo;
var_dump(get_object_vars($test));

$test->test();

?>

以上例程会输出:

array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  NULL
}
array(4) {
  ["a"]=>
  NULL
  ["b"]=>
  int(1)
  ["c"]=>
  NULL
  ["d"]=>
  NULL
}

注意:

未初始化的属性认为是不可访问的,因此不会包含在数组中。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top