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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等