For me, the question is: check if the method exists in the extended class, but not the parent class from the parent class
Taking the above example from Mikhail Elantsev, it will look something like this: -
class A { function a() { } function b() { } public function own_method( $method_name ) { if (method_exists($this, $method_name)) { return true; } else return false; } } class B extends A { function b() { } function c() { } } $b = new B; var_dump($b->own_method('c'));
Why do you need this?
I have a BaseModel class in which I format tooltips for attributes in an extended model:
class BaseModel { public function attributeHelp($attributeName) { if (method_exists($this, 'hints') && array_key_exists($attributeName, $this->hints())) { return '<i html for nicely formatted hint' . $this->hints()[$attributeName] . '"></i>'; } return false; } }
And then in my data model
class Customer extends BaseModel { public function hints() { return [ 'customer_name' => 'please use your fullname ...', 'some other attribute' => 'some other description' ]; } }
source share