Reputation: 26281
I understand that I should make a method "protected" when I want it to be only available to all classes that extend the current class as well as the current class.
Okay, grandChildClass::method2() should be protected since grandchild is extended from child.
But what should it be if accessed from a parent class such as parentClass::method2()?
class parentClass
{
public function method1() {$this->method2();}
}
class childClass extends parentClass
{
protected function method2() {}
}
class grandChildClass extends childClass
{
public function method3() {$this->method2();}
}
Upvotes: 0
Views: 3322
Reputation: 174947
If you try to
$p = new parentClass;
$p->method1();
You'd get a fatal error for undefined method.
Fatal error: Call to undefined method
parentClass::method2()
in ... on line ...
However, this will work fine:
$c = new childClass;
$c->method1();
$g = new grandChildClass;
$g->method1();
$g->method3();
All of these will call method2
as defined on childClass
.
Upvotes: 2