A question about class inheritance

Source: Internet
Author: User
The following code: {code...} outputs the following code: {code...} Instead of {code:

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 ();

Why is output

Bar::testPrivate Foo::testPubli

Instead

Foo::testPrivate Foo::testPubli

Reply content:

The following code:

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 ();

Why is output

Bar::testPrivate Foo::testPubli

Instead

Foo::testPrivate Foo::testPubli

The testPublic () method in the Foo class overrides testPublic () of the parent Bar ();
The property of testPrivate () is private and can only be accessed by the same class object. Even if it is not the same instance, since it can only be accessed by the same class object, it will certainly not be overwritten, so the above output will appear.

Private methods are not inherited.test()Methods are defined in the parent class. The referenced private methods always call the parent class, regardless of whether the subclass defines methods with the same name.

This method is related to the domain.BarOftestMethod,phpOfcalling scopeYesFoo, SopublicWill be overwritten, andprivateOnly according to the visibilityBar.

The private method subclass in the parent class cannot be rewritten or called.

Although the Foo class overrides two methods, it does not overwrite the test () method.

When the test method of the parent class is executed, $ this is the private method of the parent class. That is, if the parent class has this private method, the parent class is called. If the parent class does not exist, the Child class is used.

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.