Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取子类的所有方法?_Php_Oop_Class_Object - Fatal编程技术网

Php 如何获取子类的所有方法?

Php 如何获取子类的所有方法?,php,oop,class,object,Php,Oop,Class,Object,我正在为其他对象编写一个对象实例。 现在我需要验证一个实例化的对象 我使用的代码是正确的,但是这些对象是另一个对象的子对象,所以请进一步返回父对象的方法 代码: <?php class MyParentClass { ... $objectName = "subClassExample"; $obj = new $objectName(); print_r( get_class_methods( $obj ) ); ... } ?> Ar

我正在为其他对象编写一个对象实例。 现在我需要验证一个实例化的对象

我使用的代码是正确的,但是这些对象是另一个对象的子对象,所以请进一步返回父对象的方法

代码:

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

我希望我能帮忙,我真的很感激。 你好

旁白:对不起,我的英语不是我的语言,我会说西班牙语和挪威语。

你可以通过以下方式来实现:

输出:

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\u class\u方法
返回所有类型的方法(public、protected等)

什么是
$objectName
?它是哪个对象?$objectName是对象名。。。例如:$objectName='SubassessExample';太好了,谢谢!事实证明了这一点,但只有在文档中才能看到,就像我(:-})