php判斷類的方法是否存在


method_exists方法很多人都用過,但是今天用到method_exists方法時候發現有一個類,有很多子類,而我又只想改一個子類的方法,並不想影響到這個父類,所以就有了以下的測試

測試

<?php
class testFunction{
    public function __construct(){
        echo 'test start';
        if(method_exists($this,'setSeoTitle')){
            $this->setSeoTitle();
        }
        
        echo 'test end';
    }
   
  
    
}
class testFunctionChild extends testFunction{
      function setSeoTitle(){
       echo 'setSeoTitle is start'.PHP_EOL;
    }
}

new testFunctionChild();

結果

setSeoTitle is start
test end

總結

測試發現,只要是繼承了這個父類的所有子類的方法都是可以使用method_exists判斷的(當然私有方法除外)


免責聲明!

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



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