检查扩展类中是否存在方法,但不存在父类


Check if a method exists in an extended class but not parent class

使用method_exists,它检查所有方法,包括父类。

示例:

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

如何检查该方法是否只存在于当前类而不存在于父类(即Tootsie)?

由于v.4.0.5 php有get_parent_class()方法,该方法返回父类。所以你可以处理它而不需要重新思考:

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

如果你需要知道该方法是否存在于给定的子类中,而不管父类中是否存在,你可以这样做:

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

您可以使用反射

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

有关更多信息,ReflectionClass

基于上面的@失眠答案,但在该方法根本不存在的情况下工作:

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

对我来说,问题是检查扩展类中是否存在方法,而不是父类中的父类

以Elantcev Mikhail的上述例子为例,它看起来更像这样:-

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

你为什么要这么做?

我有一个类BaseModel,我在其中格式化扩展模型中属性的提示:

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

然后在我的数据模型中

class Customer extends BaseModel
{
    public function hints()
    {
        return [
           'customer_name' => 'please use your fullname ...',
           'some other attribute' => 'some other description'
        ];
    }
}

这是我的解决方案,使用反射并检查反射方法中涉及的实际类。

<?php
class Base {
    public function BaseOnly() {
    }
    public function Extended() {
    }
}
class Child extends Base {
    public function ChildOnly() {
    }
    public function Extended() {
    }
}
function childMethodExists($baseClass, $childClass, $methodName) {
    if (!method_exists($childClass, $methodName)) {
        return false; // doesn't exist in the child class or base class
    }
    if (!method_exists($baseClass, $methodName)) {
        return true; // only exists on child class, as otherwise it would have returned above
    }
    // now to check if it is overloaded
    $baseMethod = new ReflectionMethod($baseClass, $methodName);
    $childMethod = new ReflectionMethod($childClass, $methodName);
    return $childMethod->class !== $baseMethod->class;
}
var_dump(childMethodExists(Base::class, Child::class, 'BaseOnly')); // false
var_dump(childMethodExists(Base::class, Child::class, 'ChildOnly')); // true
var_dump(childMethodExists(Base::class, Child::class, 'Neither')); // false
var_dump(childMethodExists(Base::class, Child::class, 'Extended')); // true