PHP类与对象中的private访问控制的疑问
程序员文章站
2022-06-03 18:46:42
...
在手册中遇到了一个没想明白的问题,记录一下: 1 class Bar 2 { 3 public function test() { 4 $this - testPrivate(); 5 $this - testPublic(); 6 } 7 8 public function testPublic() { 9 echo "Bar::testPublic\n" ; 10 } 11 12 private function testPr
在手册中遇到了一个没想明白的问题,记录一下:
1 class Bar 2 { 3 public function test() { 4 $this->testPrivate(); 5 $this->testPublic(); 6 } 7 8 public function testPublic() { 9 echo "Bar::testPublic\n"; 10 } 11 12 private function testPrivate() { 13 echo "Bar::testPrivate\n"; 14 } 15 } 16 17 class Foo extends Bar 18 { 19 public function testPublic() { 20 echo "Foo::testPublic\n"; 21 } 22 23 private function testPrivate() { 24 echo "Foo::testPrivate\n"; 25 } 26 } 27 28 $myFoo = new foo(); 29 $myFoo->test(); // Bar::testPrivate 30 // Foo::testPublic
为啥第一行会输出Bar::testPrivate呢?
一些资料:
http://www.haidx.com/php-private.html
还有php官网上,关于这段代码的贡献者回复中,也找到了一条:
http://www.php.net/manual/zh/language.oop5.visibility.php#87413