PHP 快速導覽 - 核心延伸函數 類別、物件相關 property_exists()

內建函數 (function) property_exists() 判斷類別 (class) 是否有指定屬性

函數說明
bool property_exists(mixed $class, string $property)$class 為類別變數, $property 為屬性名稱。


參數 (parameter) 為類別變數與屬性名稱,須注意回傳的是布林值。


舉例如下
<?php
class Apple {
    public $demo = 0;
}

$a = new Apple();

if (property_exists($a, 'demo')) {
    echo "Yes, \$demo is an Apple's property.\n";
}
else {
    echo "No, \$demo is not an Apple's property.\n";
}

if (property_exists($a, 'talk')) {
    echo "Yes, \$talk is an Apple's property.\n";
}
else {
    echo "No, \$talk is not an Apple's property.\n";
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:object16.php
    功能:示範 PHP 程式 
    作者:張凱慶
    時間:西元 2013 年 2 月 */
?>


執行結果如下



中英文術語對照
函數function
陣列array
參數parameter


您可以繼續參考
類別、物件相關函數


相關目錄
回 PHP 快速導覽
回 PHP 教材
回首頁


參考資料
http://www.php.net/manual/en/function.property-exists.php

沒有留言: