在手册中遇到了一个没想明白的问题,记录一下:

 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

相关文章:

  • 2021-09-13
  • 2021-08-22
  • 2022-01-26
  • 2022-12-23
  • 2021-05-31
  • 2021-12-11
  • 2022-12-23
猜你喜欢
  • 2021-06-12
  • 2022-12-23
  • 2021-10-01
  • 2021-05-26
  • 2022-12-23
相关资源
相似解决方案