Check if the method exists in the extended class, but not in the parent class

Using method_exists it checks all methods, including the parent class.

Example:

 class Toot { function Good() {} } class Tootsie extends Toot { function Bad() {} } function testMethodExists() { // true var_dump(method_exists('Toot', 'Good')); // false var_dump(method_exists('Toot', 'Bad')); // true var_dump(method_exists('Tootsie', 'Good')); // true var_dump(method_exists('Tootsie', 'Bad')); } 

How can I verify that a method exists only for the current class, and not for the parent class (i.e. Tootsie )?

+6
source share
5 answers

Since v. 4.0.5 php has a get_parent_class () method that returns the parent class. This way you can handle this without dropping:

 class A { function a() { /* ... */} function b() { /* ... */} } class B extends A { function b() { /* ... */} function c() { /* ... */} } function own_method($class_name, $method_name) { if (method_exists($class_name, $method_name)) { $parent_class = get_parent_class($class_name); if ($parent_class !== false) return !method_exists($parent_class, $method_name); return true; } else return false; } var_dump(own_method('B', 'a')); // false var_dump(own_method('B', 'b')); // false var_dump(own_method('B', 'c')); // true 
+6
source

you can use reflection

 $refl = new ReflectionClass($class_name); if($refl->hasMethod($method_name)){ //do your stuff here } 

for more information ReflectionClass

+1
source

If you need to know if a method exists in this child regardless of whether it exists in the parent class, you can do this as follows:

 $reflectionClass = new ReflectionClass($class_name); if ($reflectionClass->getMethod($method_name)->class == $class_name) { // do something } 
+1
source

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')); // true 

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' ]; } } 
0
source

Based on @sleepless answer above, but works if the method does not exist at all:

 function methodImplementedInClass($className, $methodName) { // If this class doesn't have the method at all, return false if (!method_exists($className, $methodName)) { return false; } // Now check if the method was implemented in this class or in some parent return (new ReflectionClass($className))->getMethod($methodName)->class == $className; } 
0
source

All Articles