Inheritance in PHP OOP

I
Trusted SSL from just $3.44
Access Modifiers in PHP
Constants in PHP OOP

What is Inheritance?

When a class derives from another class, it is known as inheritance. Here are some important terms related to inheritance in PHP.

  • Parent Class – The class from which the other classes are derived is called parent class. It is also known as base class.
  • Child Class – The class that is derived from another class is called child class. There are some other names for it also like leaf class or derived class.
Inheritance in PHP

How to inherit a class in PHP?

A class can be inherited by using extends keyword. Look at the following example.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

// Apple is inherited from Fruit
class Apple extends Fruit {
  public function message() {
    echo "I am from Fruit class or Apple one? ";
  }
}
$apple= new Apple("Apple", "red");
$apple->message();
$apple->intro();
?>

In the above example

  • We create an base or parent class Fruit with some properties and methods.
  • We create another class apple that extends/inherit the parent class Fruit using extends keyword.
  • Create apple instance/object and access the fruit class method using the inheritance.

Wrapping up the introduction to inheritance in PHP, we gather the following points for you.

  1. Inheritance allows the child class to access the public __construct, methods and properties of the parent class.
  2. In the above example, we can access the method of apple class using its object, as well as the methods of parent class.

Scope of protected access modifiers in Inheritance

In the previous tutorial, we learned that protected attributes/methods of a class are accessible within the class and the classes derived from it.

To understand the scope of protected access modifiers in inheritance, let’s look at the following example first.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "I am $this->name and my color is $this->color";
  }
}

class Apple extends Fruit {
  public function message() {
    echo "I am from Fruit class or Apple one?";
  }
}

$apple = new Apple("Apple", "red");
$apple->message();
$apple->intro();
?>

In the above example:

  • We create a class fruit with some public attributes and a protected method.
  • We create another class apple, extends from Fruit class and create its instance.
  • When we try to access the protected method of the Fruite class using apple class object, it gives error because, we are trying to access protected member of Fruit class outside the derived class.

Let’s move to another example, which is actually a little modified of the above example.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "I am {$this->name} and my color is {$this->color}.";
  }
}

class Apple extends Fruit {
  public function message() {
    echo "Am I a fruit or an apple? ";
    $this -> intro(); // protected
  }
}

$apple = new Apple("Apple", "red");
$strawberry->message();
?>

In the above example, the protected method of the Fruit class is accessible in the apple class because we are accessing it within the class.

Overriding in Inheritance

Overriding refers to redefining the methods of the parent class in the child classes, using the same name. Look at the following example of overriding in inheritance below.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "I am {$this->name} and my color is {$this->color}.";
  }
}

class Apple extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function intro() {
    echo "I am {$this->name}, my color is {$this->color}. Also, my weight is {$this->weight} kg.";
  }
}

$apple= new Apple("Apple", "red", 50);
$apple->intro();
?>

In the above example:

  • We create a parent class Fruit and a child class apple which extends the Fruit class and redefine the __contruct and intro methods in it.
  • Note that the child class apple defines the __construct and intro methods with same name. However, parameters can be different.
  • We create apple class intance and pass the parameters to the construct function.
  • When we call the intro method using apple instance, it calls the intro method of the apple class because we created the instance of the apple object using apple class constructor.

Role of Final Keyword in inheritance

The final keyword prevents the child/derived class from overriding the method of the parent/base class. Look at the following example to understand the use of the Final keyword.

<?php
class Fruit {
  final public function intro() {
    // code
  }
}

class Apple extends Fruit {
// error to override the intro
  public function intro() {
    // code
  }
}
?>

In the above example, the apple class tries to override the intro method. It will give an error because we have used the final keyword with the intro method of the Fruit class.

Click for an official reference to the PHP inheritance.

Conclusions

Inheritance is not only an important concept of OOP in PHP, but in any language, it plays a vital role. From the interview perspective, inheritance questions are very important. So, you must practice the inheritance on your own.

Access Modifiers in PHP
Constants in PHP OOP

Tutor Network

Share this

Learn PHP from A to Z