我正在为其他对象编写对象实例。现在我需要验证一个实例化的对象。
我使用的代码是正确的,但是对象是另一个对象的子对象,所以进一步返回父对象的方法。
代码:
<?php
class MyParentClass
{
...
$objectName = "subClassExample";
$obj = new $objectName();
print_r( get_class_methods( $obj ) );
...
}
?>
返回:Array ( [0] => __construct [1] => myMethod )
子类:
<?php
class subClassExample extends parentClass
{
public function myMethod()
{
return null;
}
}
?>
我需要返回:
Array ( [0] => myMethod )
父类:
<?php
class parentClass
{
function __construct ()
{
return null;
}
}
?>
我希望我能帮上忙,我真的很感激。问候!注::对不起,我的英语不是我的语言,我会说西班牙语和挪威语。
你可以使用PHP的ReflectionDocs:
class Foo
{
function foo() {}
}
class Bar extends Foo
{
function bar() {}
}
function get_class_methodsA($class)
{
$rc = new ReflectionClass($class);
$rm = $rc->getMethods(ReflectionMethod::IS_PUBLIC);
$functions = array();
foreach($rm as $f)
$f->class === $class && $functions[] = $f->name;
return $functions;
}
print_r(get_class_methodsA('Bar'));
输出:Array
(
[0] => bar
)
如果您只需要惟一的子类方法,则可以在子类或父类中执行此检查:
$cm = get_class_methods($this); //Get all child methods
$pm = get_class_methods(get_parent_class($this)); //Get all parent methods
$ad = array_diff($cm, $pm); //Get the diff
请记住:get_class_methods
返回所有类型的方法(public, protected等)