php中可以使用method_exists()函數(shù)來(lái)判斷類(lèi)中是否定義了指定方法,該函數(shù)可檢查類(lèi)的指定方法是否存在,語(yǔ)法“method_exists($object,$method_name)”;如果定義了則返回true,否則返回false。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
在php中,可以使用method_exists()函數(shù)來(lái)判斷類(lèi)中是否定義了指定方法。
示例:
$directory=new Directory; if(!method_exists($directory,'read')){ echo '未定義read方法!'; }
說(shuō)明:
method_exists()函數(shù)可以檢查類(lèi)的指定方法是否存在。語(yǔ)法:
method_exists(mixed $object, string $method_name): bool
該函數(shù)會(huì)檢查類(lèi)的方法是否存在于指定的 object中。
-
object:對(duì)象示例或者類(lèi)名。
-
method_name:方法名。
推薦學(xué)習(xí):《PHP視頻教程》