Check if method exists in the same class

后端 未结 4 769
闹比i
闹比i 2020-12-06 09:18

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

4条回答
  •  眼角桃花
    2020-12-06 10:02

    You can do something like this:

    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();
    

提交回复
热议问题