Understanding Method and Property Overriding in PHP Classes

The article explains PHP class inheritance, detailing how public and protected properties and methods can be overridden in child classes while private members remain unaffected, and demonstrates correct overriding practices, parameter matching, and using the parent keyword with clear code examples.

php Courses
php Courses
php Courses
Understanding Method and Property Overriding in PHP Classes

Definition: Overriding (override) occurs when a subclass defines a member with the same name as one in its parent class, allowing the subclass to replace the parent’s member, typically to extend or modify business logic.

Property overriding: Whether a property is public or protected, once overridden, the parent’s property no longer exists in the child; private properties are not lost because they are not inherited.

Example of property overriding:

<?php
class A {
    public $name = '张三';
    protected $sex = 'man';
    private $age = '25';
    public function getName(){ echo __CLASS__, '<br/>'; echo $this->name."<br>"; }
    protected function getSex(){ echo __CLASS__, '<br/>'; echo $this->sex."<br>"; }
    private function getAge(){ echo __CLASS__, '<br/>'; echo $this->age."<br>"; }
}
class B extends A {
    public $name = '王五';
    protected $sex = 'woman';
    private $age = '26';
    public function getAll(){
        echo $this->name."<br>";
        echo $this->sex."<br>";
        echo $this->age."<br>";
    }
}
$a = new B();
var_dump($a);
$a->getAll(); // 王五 woman 26
?>

The dump shows that the public and protected properties are replaced by the child’s values, while both the child’s and parent’s private $age properties exist separately.

Conclusion: Public and protected properties are directly overridden; private properties are not inherited and therefore remain unaffected. Public and protected methods can be overridden, but private methods cannot because they are not inherited.

Method overriding requirements:

The visibility of the overriding method cannot be more restrictive than the parent’s method (e.g., a private method cannot override a protected one).

The overriding method must have the same parameter list as the parent method.

Private methods are not subject to these rules because they are not inherited.

Example of correct method overriding with matching parameters:

<?php
class A {
    protected function show(){ echo __CLASS__, '<br/>'; }
}
class B extends A {
    protected function show(){}          // allowed
    public function show(){}             // allowed
    private function show(){}           // error: more restrictive
}
?>

Example of parameter mismatch (error):

<?php
class A { protected function show(){} }
class B extends A {
    public function show($a){} // error: parameter list differs
}
?>

Since private methods are not inherited, redefining them in a subclass does not constitute overriding and causes no error:

<?php
class A { private function show(){} }
class B extends A {
    private function show($name){ echo $name.'<br/>'; }
}
?>

When a subclass wants to retain the parent’s behavior while extending it, the parent keyword is used:

<?php
class A { protected function show(){ echo __CLASS__, '<br/>'; } }
class B extends A {
    public function show(){
        parent::show(); // call parent implementation
        // additional logic
        echo __CLASS__, '<br/>'; 
    }
}
?>

The parent keyword can access static properties, static methods, class constants, and regular (non‑private) methods, but not private properties or methods of the parent class.

Overall, the article demonstrates how PHP handles overriding of properties and methods, the constraints involved, and best practices for using parent to extend inherited functionality.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPMethod Overridingproperty overriding
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.