ฟังก์ชั่น property_exists()
ความหมายของฟังก์ชั่นและการใช้งาน
การตรวจสอบว่า object หรือ class ได้มีการกำหนดไว้หรือยัง ด้วยฟังก์ชั่น property_exists()
รูปแบบการเขียน (Syntax)
bool property_exists ( mixed $class , string $property )
ตัวอย่างที่ 1
<?php
class myClass {
public $mine;
private $xpto;
static protected $test;
static function test() {
var_dump(property_exists('myClass', 'xpto')); //true
}
}
var_dump(property_exists('myClass', 'mine')); //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto')); //true, as of PHP 5.3.0
var_dump(property_exists('myClass', 'bar')); //false
var_dump(property_exists('myClass', 'test')); //true, as of PHP 5.3.0
myClass::test();
?>
ตัวอย่างที่ 2
<?php
class TestClass {
public $declared = null;
}
$testObject = new TestClass;
var_dump(property_exists("TestClass", "dynamic")); // boolean false, as expected
var_dump(property_exists($testObject, "dynamic")); // boolean false, same as above
$testObject->dynamic = null;
var_dump(property_exists($testObject, "dynamic")); // boolean true
unset($testObject->dynamic);
var_dump(property_exists($testObject, "dynamic")); // boolean false, again.
var_dump(property_exists($testObject, "declared")); // boolean true, as espected
unset($testObject->declared);
var_dump(property_exists($testObject, "declared")); // boolean true, even if has been unset()