Skip to content
PHP Logo

Can private methods in a PHP class be inherited by derived classes, and if not, what are the implications for object-oriented programming?

Private methods in a PHP class cannot be inherited by derived classes. This means that child classes cannot access or override private methods defined in the parent class, limiting the flexibility and reusability of the code. To overcome this limitation, you can use protected methods instead of private ones, as protected methods can be inherited by child classes.

php
class ParentClass {
    protected function myMethod() {
        echo "This is a protected method.";
    }
}

class ChildClass extends ParentClass {
    // Child class can access and override protected methods
}

$child = new ChildClass();
$child->myMethod(); // Output: This is a protected method.