A piece of code inherited by the PHP class

Source: Internet
Author: User
{Code...} is a piece of code from the php manual. How can I get the final output result? The object of the subclass Foo calls the test () method, and the test () method calls $ this-& amp; gt; testPrivate (); this $ this should be a reference of the subclass at this time, it is reasonable to call testPrivat of the subclass...
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

A piece of code from the php manual. How can I get the final output result?

The object of the subclass Foo calls the test () method, and the test () method calls $ this-> testPrivate (); this $ this should be a reference of the subclass at this time, we should call the testPrivate () method of the subclass, but actually call the testPrivate () method of the parent class, why?

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

A piece of code from the php manual. How can I get the final output result?

The object of the subclass Foo calls the test () method, and the test () method calls $ this-> testPrivate (); this $ this should be a reference of the subclass at this time, we should call the testPrivate () method of the subclass, but actually call the testPrivate () method of the parent class, why?

This is a situation where PHP dynamic binding and static binding.

Public is dynamic binding and is not bound during compilation. Therefore, when the test () method of the parent class is called at runtime, it is resolved as the public method of the subclass.
Private is private and does not inherit from sub-classes. It is bound during compilation. It is a "static" binding (similar to self after 5.3 ).

This is related to LSB and static latency binding. PHP5.3 enhances php oop with this feature.

Public: It can be inherited or called externally.
Private: it cannot be inherited or called externally.
Protected: can be inherited, but cannot be called externally.

Because you are calling$ MyFooThe test method. This method is inherited,ThisIt points to the $ myFoo instance itself.
So what is called isFooTestPublic method.
But I don't understand whyTestPrivateThe parent class is called.

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.