method_exists in parent class php

2019-06-18 04:35发布

问题:

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!

回答1:

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



回答2:

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'


回答3:

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;
    }
}


回答4:

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.



回答5:

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


回答6:

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