Home > Article > Backend Development > {{PHP}}PHP inherited access control_PHP tutorial
{{PHP}}PHP inherited access control
When talking about inheritance, you have to mention access control. Inheritance behaves differently under different access control permissions. Taking member methods as an example, we can use private and protected access modifiers to control what needs to be inherited.
private If a member is designated as private, it cannot be inherited. In fact, this method will be inherited in PHP, but it will not be accessible.
protected If a member is designated as protected, it will not be visible outside the class and can be inherited.
Look at an example in PHP:
class Base {
private function privateMethod() {
}
}
class Child extends Base{
Public function publicMethod() {
}
}
$c = new Child();
if (method_exists($c, 'privateMethod')) {
echo 1;
}else{
echo 0;
}