Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ฟังก์ชัน get_object_vars() ใน PHP

คำอธิบาย

ฟังก์ชัน get_object_vars() คืนค่าอาร์เรย์ associative ของ properties ที่เข้าถึงได้ของออบเจ็กต์ที่ระบุ

รูปแบบการใช้งาน (Syntax)

get_object_vars(object)

พารามิเตอร์

  • object — อินสแตนซ์ของออบเจ็กต์

ค่าที่คืนกลับ

คืนค่าอาร์เรย์ associative ของ properties (property ที่ยังไม่ได้กำหนดค่าจะเป็น NULL)

ตัวอย่างการใช้งาน

<?php
class Point2D {
    public $x, $y;
    public $label;
    
    function __construct($x, $y) {
        $this->x = $x;
        $this->y = $y;
    }
    
    function setLabel($label) {
        $this->label = $label;
    }
}

$p1 = new Point2D(9.675, 8.321);
print_r(get_object_vars($p1));

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

ผลลัพธ์

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