本文介紹了檢查方法是否存在于同一個類中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
因此,method_exists()
需要一個對象來查看方法是否存在.但是我想知道同一個類中是否存在一個方法.
So, method_exists()
requires an object to see if a method exists. But I want to know if a method exists from within the same class.
我有一個方法可以處理一些信息并可以接收一個動作,它運行一個方法來進一步處理該信息.我想在調用之前檢查該方法是否存在.我怎樣才能實現它?
I have a method that process some info and can receive an action, that runs a method to further process that info. I want to check if the method exists before calling it. How can I achieve it?
示例:
class Foo{
public function bar($info, $action = null){
//Process Info
$this->$action();
}
}
推薦答案
你可以這樣做:
class A{
public function foo(){
echo "foo";
}
public function bar(){
if(method_exists($this, 'foo')){
echo "method exists";
}else{
echo "method does not exist";
}
}
}
$obj = new A;
$obj->bar();
這篇關于檢查方法是否存在于同一個類中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!