i

PHP Tutorial

PHP-Inheritance

You can derive a class from another class in Object Oriented Programming and the process is known as Inheritance. The derived class is also called child class and it will inherit all the public and protected methods and properties from the parent class or the class from which it is derived. A derived class can also have its own properties and methods.

You can define an inherited class using extends keyword.

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 function message() {
    echo "The surgeon are doctors experienced in surgery";
  }
}
$Surgeon1 = new Surgeon1("Sebastian");
$Surgeon1 ->message();
$Surgeon1 ->intro();
?>

The Inherited class Surgeon can access the public property name and method intro() of the derived class and also has its own method named message().