PHP从子类中获取重写的方法 [英] PHP get overridden methods from child class
本文介绍了PHP从子类中获取重写的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于以下情况:
<?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() {}
}
如何获取在ChildClass中重写的方法(最好是类var)的列表?
How can I get a list of methods (and preferably class vars) that are overridden in ChildClass?
谢谢, 乔
已修复错误扩展.任何方法,而不仅仅是公共方法.
Fixed bad extends. Any methods, not just public ones.
推荐答案
反射是正确的,但是您必须这样做:
Reflection is correct, but you would have to do it like this:
$child = new ReflectionClass('ChildClass');
// find all public and protected methods in ParentClass
$parentMethods = $child->getParentClass()->getMethods(
ReflectionMethod::IS_PUBLIC ^ ReflectionMethod::IS_PROTECTED
);
// find all parent methods 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
}
}
属性相同,只是您将使用getProperties()
.
Same for Properties, just you would use getProperties()
instead.
这篇关于PHP从子类中获取重写的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文