Home » Php » method_exists in parent class php

method_exists in parent class php

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to use the php function method_exists, but I need to check if the method exists in the parent class of an object.

so:

class Parent
{
    public function myFunction()
    {
        /* ... */
    }
}

class Child extends Parent
{
    /* ... */
}

$myChild = new Child();

if (method_exists($myChild, 'myFunction'))
{
    /* ... */
}

if (method_exists(Parent, 'myFunction'))
{
    /* ... */
}

if (is_callable(array('Parent', 'myFunction'))
{
    /* ... */
}

But none of the above are working. I’m not sure what to try next.

Thanks for any help!

How to&Answers:

Class child must extend the parent in that case

class Parent
{
   public function hello()
   {

   }
}

class Child extends Parent
{

}

$child = new Child();

if(method_exists($child,"hello"))
{
    $child->hello();
}

Update This would have the same effect as method_exists I believe.

function parent_method_exists($object,$method)
{
    foreach(class_parents($object) as $parent)
    {
        if(method_exists($parent,$method))
        {
           return true;
        }
    }
    return false;
}

if(method_exists($child,"hello") || parent_method_exists($object,"hello"))
{
    $child->hello();
}

Just updated from Wrikken‘s post

Answer:

You should use PHP’s Reflection API:

class Parend
{
  public function myFunction()
  {

  }
}

class Child extends Parend{}

$c = new Child();


$rc = new ReflectionClass($c);
var_dump($rc->hasMethod('myFunction')); // true

And if you want to know in which (parent) class the method lives:

class Child2 extends Child{}

$c = new Child2();
$rc = new ReflectionClass($c);

while($rc->getParentClass())
{
    $parent = $rc->getParentClass()->name;
    $rc = new ReflectionClass($parent);
}
var_dump($parent); // 'Parend'

Answer:

If you want to know specifically if it exists in the parent, and not only in your own class:

foreach(class_parents($this) as $parent){
    if(method_exists($parent,$method){
        //do something, for instance:
        parent::$method();
        break;
    }
}

Answer:

RobertPitt is correct in that the Child class is not a child class unless it extends the Parent class. But from your original code snippet, the following should be true:

if (method_exists('Parent', 'myFunction')
{
  // True
}

Note the ‘Parent’ is in quotes, you had it unquoted. Passing the class name as a string.

Answer:

Wouldn’t method_exists and get_parent_class combined also work if done within the child class?

For instance

class Parent
{

}

class Child extends Parent
{
   public function getConfig()
   {
     $hello = (method_exists(get_parent_class($this), 'getConfig')) ? parent::getConfig() : array();
   }
}

Answer:

The excample:
if (method_exists(‘Parent’, ‘myFunction’)
does not work in PHP 5.3.5 if you like to check for a parent constructor.

But this worked for me:

class Child extends Parent {
  function __construct($argument) {
    if(method_exists(get_parent_class(),"__construct")) parent::__construct($argument)
  }
}

It calls the Parent Constructor only if the it exists in the parent class