Questions in PHP Manuals

Source: Internet
Author: User
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

Ask the great God to explain this code, the last side of the output why is this?

Reply content:

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

Ask the great God to explain this code, the last side of the output why is this?

The method of private visibility does not exist, so the calling method in which class is called the private method corresponding to that class (if it does not exist, the error is reported)

The method call of the pit father's inheritance chain: Would have knocked a lot of people around.

It is recommended to look at C speech implementation class and inheritance ... You'll understand a lot.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.