Can I change the access modifiers of any parent function in Child class?
Child classes may only "loosen" the visibility, e.g.
class ParentClass
{
protected function foo() {}
}
can be
class ChildClass extends ParentClass
{
public function foo() {}
}
but not
class ChildClass extends ParentClass
{
private function foo() {}
}
Apart from that, you can use the Reflection API to change Visibility at runtime:
though you should ask yourself why you would want to do that. Usually parent classes that hide methods or properties from their children do so for a reason.
No. It is impossible. And not only in PHP.
My own tests suggest that a child class can increase the visibility but not decrease it.
For example, suppose a parent class has a member variable declared as private
. Then a child class can define the same member variable as either private
, protected, or
public`.
Similarly, if the parent member is declared as protected
, then the child can can declare the member as protected
or public
.
Finally, if the parent declares the member as public
, then the member must be public
in all children.