property_exists() วิธีการตรวจสอบว่าอ็อบเจ็กต์หรือคลาสมีคุณสมบัติหรือไม่
ไวยากรณ์
property_exists(object, property)
พารามิเตอร์
-
วัตถุ/ คลาส − วัตถุหรือชื่อคลาส
-
ทรัพย์สิน − ชื่อทรัพย์สิน
คืนสินค้า
ฟังก์ชัน property_exists() จะคืนค่า TRUE หากคุณสมบัติมีอยู่ FALSE หากไม่มีอยู่ หรือ NULL ในกรณีที่เกิดข้อผิดพลาด
ตัวอย่าง
ต่อไปนี้เป็นตัวอย่าง −
<?php
class Demo {
public $one;
private $two;
static protected $VAL;
static function VAL() {
var_dump(property_exists('myClass', 'two'));
}
}
var_dump(property_exists('Demo', 'one'));
var_dump(property_exists(new Demo, 'one'));
?> ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ -
bool(true) bool(true)