I think the comment by omega at 2093 dot es ( http://www.php.net/manual/en/language.oop5.visibility.php#109324 ) describes the same thing. It says: "Methods defined in the parent class cannot access private methods defined in the class that inherits them. However, they can access protected methods."
In your case, the $this
object in the Bar::test()
method is of type Foo
(your var_dump proves this). Since the Foo::testPrivate()
method is private, access to it from the parent class is not possible, and the only method available to it is Bar::testPrivate()
(try commenting on the definition and you will get a fatal error). Therefore, the first output is Bar::testPrivate
.
String $this->testPublic();
calls the Foo::testPublic()
method, because $this
is of type Foo
, and the method is defined as public.
In short, private methods are only available from the class where they are defined. They can not be addressed neither from the child, nor from the parent classes.
To make an accessible method from a child or parent class, it will be protected. For example, if you create a testPrivate()
method protected in both classes, it will print Foo::testPrivate Foo::testPublic
.
source share