get_object_vars()

返回指定对象的属性和属性值的关联数组

首字母:G

函数名称:get_object_vars()

适用版本:所有PHP版本

函数描述:get_object_vars() 函数用于返回指定对象的属性和属性值的关联数组。

语法:get_object_vars(object $object): array

参数:

  • $object:必需。要获取属性的对象。

返回值:返回一个关联数组,其中键表示属性名,值表示属性值。

示例:

class Student {
 public $name;
 protected $age;
 private $grade;
 public function __construct($name, $age, $grade) {
 $this->name = $name;
 $this->age = $age;
 $this->grade = $grade;
 }
}
$student = new Student("John", 18, "A");
$properties = get_object_vars($student);
print_r($properties);

输出:

Array
(
 [name] => John
 [age] => 18
 [grade] => A
)

在上面的示例中,我们定义了一个名为Student的类,并创建了一个名为$student的对象。然后,我们使用get_object_vars()函数获取了$student对象的属性和属性值,并将结果存储在$properties变量中。最后,我们使用print_r()函数打印了$properties数组,显示了对象的属性和属性值。

同首字母函数