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.