如何解决奇怪的php oop行为,有人可以解释吗?
| 简而言之:一个类从其父类继承一个函数。然后,该函数在子级上被调用,但似乎仍具有父类的范围。这是预期的行为吗? 考虑以下代码示例:<?php
class OLTest {
private $var1 = 10;
public function getVar1() {
if (isset($this->var1)) {
return $this->var1;
} else {
return \'undefined\';
}
}
public function getVar2() {
if (isset($this->var2)) {
return $this->var2;
} else {
return \'undefined\';
}
}
}
class OLTest2 extends OLTest {
private $var1 = 11;
private $var2 = 20;
}
$oltest = new OLTest();
$oltest2 = new OLTest2();
echo \"calling parent->getVar1\\n\";
echo $oltest->getVar1() . \"\\n\";
echo \"calling parent->getVar2\\n\";
echo $oltest->getVar2() . \"\\n\";
echo \"calling child->getVar1\\n\";
echo $oltest2->getVar1() . \"\\n\";
echo \"calling child->getVar2\\n\";
echo $oltest2->getVar2() . \"\\n\";
?>
据我了解,输出应为:
calling parent->getVar1
10
calling parent->getVar2
undefined
calling child->getVar1
11
calling child->getVar2
20
我的机器上的实际输出为:
calling parent->getVar1
10
calling parent->getVar2
undefined
calling child->getVar1
10
calling child->getVar2
undefined
更令人困惑的是,两个函数中的一个“ 3”将表明作用域确实设置为子类,但无法访问该变量。
有人可以帮我清理一下吗?
编辑:我在版本5.3.3-1ubuntu9.5中使用PHP。
解决方法
不,输出应明确为
10,undefined,10,undefined
。
原因是private
的变量仅对定义它们的类(而不是上级或子类)可见。
因此,当调用child
时,其方法在父对象中定义,并且当它们解析var1
或check8ѭ时,将检查为OLTest
定义的范围。
the8ѭ也无法访问,因为声明的var2
仅在OLTest2
内部可见。
为了获得输出,您应该声明这些变量protected
或public
。
, 它似乎与“ 5”作用域有关。
尝试更改为protected
或public
,然后检查结果。
, 是的,这是因为您的财产是私人的。您正在调用的方法仍属于父类,因为您尚未重新定义它们。要使父方法能够访问子级的属性,必须在父类和子类中将它们设置为“ 13”:
class OLTest {
protected $var1 = 10;
public function getVar1() {
if (isset($this->var1)) {
return $this->var1;
} else {
return \'undefined\';
}
}
public function getVar2() {
if (isset($this->var2)) {
return $this->var2;
} else {
return \'undefined\';
}
}
}
class OLTest2 extends OLTest {
protected $var1 = 11;
protected $var2 = 20;
}
如果在父类中将其保留为“ 5”,则它将不允许子类覆盖该属性,因此属于父类的函数将访问其自己的私有属性。而且,如果您在子类中将其设为私有,则不允许父方法访问它。
如果仍然想让它们保密,则必须将方法的代码复制粘贴到子类中(是的,return parent::getVar1()
也不起作用)。
, 您不能“ 22”“插入”父类,这是您试图做的。
是的,这是正常行为。
, 因为您已将自己的var声明为私有,所以它们完全相互隐藏。因此,父母中的$var1
实际上是与孩子中的$var1
不同的变量
当您在父级上调用getVar1
时,它将使用它看到的$var1
,即来自父级的值为23的那个。类似地,父级无法看到子级的$var2
,因此该属性未定义。
当在子级上调用getVar1
和getVar2
时,方法本身是从父类继承的,其行为与从OLTest类型的对象中调用它们时的行为完全相同。
, 这是您的私有变量的范围,请尝试使用受保护的
class OLTest {
protected $var1 = 10;
public function getVar1() {
return isset($this->var1) ? $this->var1:\'undefined\';
}
public function getVar2() {
return isset($this->var2) ? $this->var2:\'undefined\';
}
}
class OLTest2 extends OLTest {
protected $var1 = 11;
protected $var2 = 20;
}
$oltest = new OLTest();
$oltest2 = new OLTest2();
echo \"calling parent->getVar1\\n\".$oltest->getVar1().\"\\n\";
echo \"calling parent->getVar2\\n\".$oltest->getVar2().\"\\n\";
echo \"calling child->getVar1\\n\".$oltest2->getVar1().\"\\n\";
echo \"calling child->getVar2\\n\".$oltest2->getVar2().\"\\n\";
结果:
calling parent->getVar1
10
calling parent->getVar2
undefined
calling child->getVar1
11
calling child->getVar2
20
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。