欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP类与对象中的private访问控制的疑问_PHP教程

程序员文章站 2022-04-28 18:05:23
...
复制代码 代码如下:

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(); // Bar::testPrivate
// Foo::testPublic

为啥第一行会输出Bar::testPrivate呢?
一些资料:
http://www.jb51.net/article/31709.htm
还有php官网上,关于这段代码的贡献者回复中,也找到了一条:
http://www.php.net/manual/zh/language.oop5.visibility.php#87413

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326157.htmlTechArticle复制代码 代码如下: class Bar { public function test() { $this-testPrivate(); $this-testPublic(); } public function testPublic() { echo "Bar::testPublic\n"; } private functi...