PHP çocuk sınıftan geçersiz yöntemleri olsun

2 Cevap php

Aşağıdaki durum göz önüne alındığında:

<?php

class ParentClass {

    public $attrA;
    public $attrB;
    public $attrC;

    public function methodA() {}
    public function methodB() {}
    public function methodC() {}

}

class ChildClass extends ParentClass {

    public $attrB;

    public function methodA() {}
}

Nasıl çocuğu olarak geçersiz yöntemlerle (ve tercihen sınıf vars) bir listesini alabilirsiniz?

Thanks, Joe

EDIT: Sabit kötü uzanır. Herhangi bir yöntem, sadece ortak olanlar.

2 Cevap

Yansıma doğru, ama böyle yapmak zorunda olurdu:

$child  = new ReflectionClass('ChildClass');

// find all public and protected methods in ParentClass
$parentMethods = $child->getParentClass()->getMethods(
    ReflectionMethod::IS_PUBLIC ^ ReflectionMethod::IS_PROTECTED
);

// find all parentmethods that were redeclared in ChildClass
foreach($parentMethods as $parentMethod) {
    $declaringClass = $child->getMethod($parentMethod->getName())
                            ->getDeclaringClass()
                            ->getName();

    if($declaringClass === $child->getName()) {
        echo $parentMethod->getName(); // print the method name
    }
}

Aynı Özellikleri için, sadece getProperties() yerine kullanmak istiyorsunuz.

Bunu başarmak için ReflectionClass kullanabilirsiniz:

$ref = new ReflectionClass('ChildClass');

print_r($ref->getMethods());
print_r($ref->getProperties());

Bu irade çıktı:

Array
(
    [0] => ReflectionMethod Object
        (
            [name] => methodA
            [class] => ChildClass
        )

)

Array
(
    [0] => ReflectionProperty Object
        (
            [name] => attrB
            [class] => ChildClass
        )

)

Yansıması daha yararlı bilgi için kılavuzuna bakın: http://uk3.php.net/manual/en/class.reflectionclass.php