i

PHP Tutorial

PHP-Overriding Inherited Methods

When you define the methods again in the child class that are already used in the parent class, the inherited methods can be overridden.

For example:

class Doctor {
  public $name;
  public function __construct($name) {
    $this->name = $name;
  }
  public function intro() {
    echo "The Doctor name is {$this->name}

}"; } }

// Surgeon is inherited from Doctor
class Surgeon extends Doctor{

  public $age;

  public function __construct($name, $age) {

   $this -> name = $name;

   $this -> age = $age;

}
  public function into() {
    echo "The surgeon name is {this->name} and age is {this-> age}”;
  }
}
$Surgeon1 = new Surgeon1("Sebastian", 35);
$Surgeon1 ->intro();
?>

Intro() method in derived class will override the parent class method intro() and the output will be : The surgeon name is Sebastian and age is 35.