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

內建函數 (function) method_exists() 判斷類別 (class) 是否有指定方法

函數說明
bool method_exists(mixed $object, string $method_name)$object 為變數, $method_name 為方法名稱。


參數 (parameter) 為變數與方法名稱,須注意回傳的是布林值。


舉例如下
<?php
class Apple {
    function do_something() {
        echo "something wrong\n";
    }
}

$a = new Apple();

if (method_exists($a, 'do_something')) {
    echo "Yes, do_something() is an Apple's method.\n";
}
else {
    echo "No, do_something() is not an Apple's method.\n";
}

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

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


執行結果如下



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


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


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


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

沒有留言: