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

ตรวจสอบ Property ใน Object/Class ด้วย PHP (property_exists vs isset)

PHP มี 2 วิธีหลักตรวจสอบ Property: property_exists() และ isset() พฤติกรรมต่างกันสำคัญ

property_exists()

ตรวจสอบว่า Property มีอยู่จริงใน Class/Object รวมถึง Private/Protected/Static

property_exists(mixed $class, string $property): bool

// $class = Class Name (string) หรือ Object Instance
// คืนค่า true ถ้า Property ถูกประกาศ (แม้จะเป็น null)

isset()

ตรวจสอบว่า Property มีอยู่และไม่ใช่ null (เข้าถึงได้เฉพาะ Public/Visible Property)

isset(mixed $var): bool

// คืนค่า false ถ้า Property ไม่มี, หรือเป็น null
// สำหรับ Private/Protected: ใช้ได้เฉพาะใน Scope ที่เข้าถึงได้

ตัวอย่างเปรียบเทียบ

class Demo {
    public $publicProp = 'public';
    protected $protectedProp = 'protected';
    private $privateProp = 'private';
    public $nullProp = null;
    public $undefinedProp; // ไม่ได้ Initialize
}

$obj = new Demo();

// property_exists: ตรวจสอบ Declaration
var_dump(property_exists($obj, 'publicProp'));      // true
var_dump(property_exists($obj, 'protectedProp'));   // true (PHP 5.3+)
var_dump(property_exists($obj, 'privateProp'));     // true (PHP 5.3+)
var_dump(property_exists($obj, 'nullProp'));        // true (แม้เป็น null)
var_dump(property_exists($obj, 'undefinedProp'));   // true (ประกาศไว้ แม้ไม่มีค่า)
var_dump(property_exists($obj, 'nonExistent'));     // false

// isset: ตรวจสอบ Existence + Not Null
var_dump(isset($obj->publicProp));      // true
var_dump(isset($obj->protectedProp));   // Error: Cannot access protected (นอก Class)
var_dump(isset($obj->privateProp));     // Error: Cannot access private (นอก Class)
var_dump(isset($obj->nullProp));        // false (เป็น null)
var_dump(isset($obj->undefinedProp));   // false (ไม่ได้ set ค่า)
var_dump(isset($obj->nonExistent));     // false

Static Property

class Config {
    public static $appName = 'MyApp';
    public static $debug = null;
}

var_dump(property_exists('Config', 'appName')); // true
var_dump(property_exists('Config', 'debug'));   // true (แม้เป็น null)
var_dump(isset(Config::$appName));              // true
var_dump(isset(Config::$debug));                // false (เป็น null)

Magic __get / __isset

class Magic {
    private $data = ['name' => 'John'];
    
    public function __get($name) {
        return $this->data[$name] ?? null;
    }
    
    public function __isset($name) {
        return isset($this->data[$name]);
    }
}

$obj = new Magic();
var_dump(property_exists($obj, 'name')); // false (ไม่ได้ประกาศ Property จริง)
var_dump(isset($obj->name));             // true (__isset ถูกเรียก)

สรุป: ใช้ตัวไหนเมื่อไหร่?

สถานการณ์แนะนำเหตุผล
เช็คว่า Class/Object มี Property นี้ประกาศไว้หรือไม่property_exists()รวม Private/Protected/Static/Null
เช็คว่า Property มีค่า (ไม่ใช่ null) และเข้าถึงได้isset()ใช้งานจริงส่วนใหญ่
Dynamic Property (PHP 8.2+ Deprecated)property_exists()ตรวจสอบก่อน Set
Magic __get/__issetisset()Trigger __isset()

Best Practice: Helper Function

function hasProperty($object, string $property): bool {
    // รองรับ Object และ Class Name
    if (is_object($object) || is_string($object)) {
        return property_exists($object, $property);
    }
    return false;
}

function hasValue($object, string $property): bool {
    // ตรวจสอบว่ามี Property และไม่ใช่ null
    return property_exists($object, $property) && isset($object->$property);
    // หมายเหตุ: isset() จะทำงานได้เฉพาะ Public/Visible Property
}