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判斷的(當然私有方法除外)