method_存在于父类php中

发布于 2024-09-10 14:44:56 字数 482 浏览 3 评论 0原文

我正在尝试使用 php 函数 method_exists,但我需要检查该方法是否存在于对象的父类中。

所以:

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'))
{
    /* ... */
}

但是以上都不起作用。我不知道接下来要尝试什么。

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

╭ゆ眷念 2024-09-17 14:44:57

如果在子类中完成,method_exists 和 get_parent_class 组合起来是否也可以工作?

例如

class Parent
{

}

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

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();
   }
}
慢慢从新开始 2024-09-17 14:44:57

RobertPitt 的说法是正确的,Child 类不是子类,除非它扩展了 Parent 类。但从您的原始代码片段来看,以下内容应该是正确的:

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

请注意,“Parent”用引号引起来,您没有将其引号括起来。将类名作为字符串传递。

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.

初见 2024-09-17 14:44:57

例子:
if (method_exists('父级', 'myFunction')
如果您想检查父构造函数,则它在 PHP 5.3.5 中不起作用。

但这对我有用:

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

只有当它存在于父类中时,它才会调用父构造函数

The example:
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

不再让梦枯萎 2024-09-17 14:44:56

在这种情况下,子类必须扩展父类

class Parent
{
   public function hello()
   {

   }
}

class Child extends Parent
{

}

$child = new Child();

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

Update 我相信这与 method_exists 具有相同的效果。

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

刚刚从 Wrikken 的帖子更新

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

七度光 2024-09-17 14:44:56

您应该使用 PHP 的 Reflection API:

class Parend
{
  public function myFunction()
  {

  }
}

class Child extends Parend{}

$c = new Child();


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

如果您想了解 该方法所在的(父)类:

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'

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'
裸钻 2024-09-17 14:44:56

如果您想具体知道它是否存在于父类中,而不是仅存在于您自己的类中:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文