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

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

คำอธิบาย

ฟังก์ชัน property_exists() ตรวจสอบว่าคลาสหรือออบเจ็กต์มี property (คุณสมบัติ) ที่ระบุหรือไม่

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

property_exists(object|class, property)

พารามิเตอร์

  • object|class — ออบเจ็กต์หรือชื่อคลาส
  • property — ชื่อ property ที่ต้องการตรวจสอบ

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

คืนค่า TRUE หาก property มีอยู่, FALSE หากไม่มี, NULL หากเกิดข้อผิดพลาด

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

<?php
class Demo {
    public $one;
    private $two;
    protected static $VAL;
}

var_dump(property_exists('Demo', 'one'));      // true (public)
var_dump(property_exists('Demo', 'two'));      // true (private - ตรวจสอบได้)
var_dump(property_exists('Demo', 'three'));    // false
var_dump(property_exists(new Demo, 'one'));    // true
?>

ผลลัพธ์

bool(true)
bool(true)
bool(false)
bool(true)