douqiaotong8682 2016-08-09 18:36
浏览 92
已采纳

为什么调用Foo类的test()函数(扩展Bar类)会返回两个类混合的结果?

It may sound silly, but I'm new in PHP. I was learning from the documentation about access specifiers when I came to this section.

class Bar {
    public function __construct() {
        echo "Bar::constructor<br />";
    }
    public function test() {
        $this->PublicTest();
        $this->PrivateTest();
        $this->protectedTest();
    }
    public function PublicTest(){
        echo "Bar::testPublic<br />";
    }
    private function PrivateTest() {
        echo "Bar::testPrivate<br />";
    }
    protected function ProtectedTest() {
        echo "Bar::testProtected<br />";
    }
}

class Foo extends Bar {
    public function __construct() {
        echo "Foo::constructor<br />";
    }
    public function PublicTest() {
        echo "Foo::testPublic<br />";
    }
    private function PrivateTest() {
        echo "Foo::testPrivate<br />";
    }
    protected function ProtectedTest() {
        echo "Foo::testProtected<br />";
    }
}

$myFoo = new Foo();
$myFoo->test();

?>

This produces output as:

Foo::constructor
Foo::testPublic
Bar::testPrivate
Foo::testProtected

Why does it prints from Bar class for private function while it prints from Foo class for public and protected function? Since, i don't have test() function in Foo class, it accesses the test() function from Bar class.

Where does $this pointer point to? Does it point to the function of Foo class or functions of Bar class? I'm really confused here. Can someone please explain this to me? Any help would be so much appreciated.

  • 写回答

2条回答 默认 最新

  • dpwqicw157673 2016-08-18 21:25
    关注

    That's because of the visibility of each class/function. When we call test() at first, using this:

    $myFoo = new Foo();
    $myFoo->test();
    

    We call the test() inside Bar class, since Foo extends Bar and it uses test() of Bar because it's public and is visible in class Foo. Now, inside foo() of Bar class, these 3 functions are called:

    $this->PublicTest();
    $this->PrivateTest();
    $this->protectedTest();
    

    Here, we are inside Bar class right now and hence it can see PrivateTest() and ProtectedTest() of it's own class only. Because the visibility of these functions are set to Private and Protected. But in case of PublicTest(), it can see 2 functions. Because PublicTest() is set to Public visibility for both Foo and Bar class. Here, Public function of Bar class is chosen although Foo class has its own PublicTest() because of following reason:

    Since, we are using $this handler, which always reference to the object currently being used (which is of Foo class in this case), it chooses PublicTest() of Foo class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计