0
<?php
class S {
public $a = 'a';
protected $b = 'b';
private $c = 'c';
function get_last_child_public_properties() {
/* HOW TO */
}
}
class A extends S
{
public $d = 'd';
protected $e = 'e';
private $f = 'f';
}
class B extends A
{
public $g = 'public_g';
protected $h = 'h';
private $i = 'i';
public $j = 'public_j';
}
$b = new B();
$props = $b->get_last_child_public_properties();
/**
* I expect $props to be equals to :
* Array : ['g' => 'public_g', 'j' => 'public_j']
*/
如果您**只有**想要父母的公共属性,您可以使用此代码:'$ reflect = new ReflectionClass(__ CLASS__);返回$ reflect-> GetProperties(ReflectionProperty :: IS_PUBLIC);'在你的函数'get_last_child_public_properties()'中。 ('var_dump'结果)。否则,你可以递归地检查每个类,看看父类是否存在,如果没有获得公共属性(因为它是最后一个类,并且不会继承其他任何东西) - 尽管我不知道有什么用途? – ctwheels