确定一个变量是否只属于PHP中的一个子类



我有一个父类和一个子类。通过父类中的一个函数,我循环遍历该类的成员,并为每个成员设置值。然而,当我遍历类成员时,我会对所有成员(父类和子类的成员(进行迭代,如下所示:

在父类中:

foreach($this as $member_name => $member_value) {
if(property_exists(get_class($this),$member_name) && isset($member_value))
//do something....
}//end foreach

然而,上面的代码将PARENT类成员变量视为子类的属性。父类中的成员变量被声明为受保护的,我无法更改这一点,所以我理解为什么会发生这种情况。我不能将父类的成员声明为私有(这将解决我的问题(!

我的问题是:有没有一种方法可以确定成员变量是否属于子类,并且只属于父类中的子类,而不声明所有父成员变量为私有

一个解决方案可以是使用反射来获取有关属性的信息。

<?php
class A {
protected $a = 1;
protected $b = 2;
public function aa() {
echo 'called class: '.get_class($this).PHP_EOL;
$r = new ReflectionClass($this);
$properties = [];
foreach($r->getProperties() as $property) {
$properties[$property->name] = $property->class;
}
echo 'properties info:'.PHP_EOL;
var_dump($properties);
echo 'iterate properties:'.PHP_EOL;
foreach ($this as $k => $v) {
echo $k.' => '.$v.', in: '.$properties[$k].', own: '.($properties[$k] === __CLASS__ ? 'yes': 'no').PHP_EOL;
}
}
}
class B extends A {
protected $c = 3;
}
$a = new A();
$a->aa();
$b = new B();
$b->aa();

输出:

called class: A
properties info:
array(2) {
["a"]=>
string(1) "A"
["b"]=>
string(1) "A"
}
iterate properties:
a => 1, in: A, own: yes
b => 2, in: A, own: yes
called class: B
properties info:
array(3) {
["c"]=>
string(1) "B"
["a"]=>
string(1) "A"
["b"]=>
string(1) "A"
}
iterate properties:
c => 3, in: B, own: no
a => 1, in: A, own: yes
b => 2, in: A, own: yes

因此,在这个例子中,我们测试父$a和子$b,并且从父方法aa我们总是有关于谁是";所有者;财产的所有权。

解决方案2:代替带反射的aa方法(如果你不需要知道谁是所有者,只需要知道它是否是父属性(,试试bb方法:

public function bb() {
echo 'called class: '.get_class($this).PHP_EOL;
foreach ($this as $k => $v) {
echo $k.' '.(property_exists(__CLASS__,$k) ? 'own' : 'not own').PHP_EOL;
}
}

因此,检查类级别上是否存在属性,输出:

called class: A
a own
b own
called class: B
c not own
a own
b own

我知道你在问什么,下面是";忽略";父类的成员:

foreach($this as $member_name => $member_value) {
if(property_exists(get_class($this),$member_name) && isset($member_value) && !property_exists(__CLASS__,$member_name))
//do something....
}//end foreach

"if"中的第三条语句将筛选出属于父类的成员。

最新更新