我正在尝试使用 php 函数 method_exists,但我需要检查该方法是否存在于对象的父类中。
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'))
{
/* ... */
}
但以上都不起作用。我不知道接下来要尝试什么。
谢谢你的帮助!
在这种情况下,子类必须扩展父类
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's post
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)