[英]PHP Object Oriented Programming Oops Issues
我正在尝试实现类似于以下的代码,但无法理解一个问题,据我的理解,它应该已经打印了这样的数据:
Foo::testPrivate
Foo::testPublic
但其输出显示为::
Bar::testPrivate
Foo::testPublic
代码是:
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new Foo();
$myFoo->test();
有人可以解释一下吗?
根据我的理解
显示输出是正确的,因为您创建了“ Foo”类的对象,然后在调用test()函数之后在“ Bar”类中
在Bar类的test()函数中,使用“ this”关键字调用testPrivate(),因此在同一类中调用该函数,并且testPrivate()也是私有的,因此显示结果如下:
酒吧:: testPrivate
富:: testPublic
富:: testPrivate
富:: testPublic
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.