PHP判斷{函數/類/方法/屬性}是否存在


1.php判斷系統函數或自己寫的函數是否存在

bool function_exists ( string $function_name ) 判斷函數是否已經定義,例如:

if(function_exists('curl_init')){
    curl_init();
}else{
    echo 'not function curl_init';
}

2.php判斷類是否存在

bool class_exists ( string $class_name [, bool $autoload = true ] ) 檢查一個類是否已經定義,一定以返回true,否則返回false,例如:

if(class_exists('MySQL')){
    $myclass=new MySQL();
}

3.php判斷類里面的某個方法是否已經定義

bool method_exists ( mixed $object , string $method_name ) 檢查類的方法是否存在,例如:

$directory=new Directory;
if(!method_exists($directory,'read')){
    echo '未定義read方法!';
}

4.php 判斷類里面的某個屬性是否已經定義

bool property_exists ( mixed $class , string $property )檢查類的屬性是否存在,例如:

$directory=new Directory;
if(!property_exists($directory,'li')){
    echo '未定義li屬性!';
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM