Inheritance in PHP Object Oriented Programming

Spread the love

Inheritance in PHP

What is Inheritance?

In general, Inheritance in PHP is a concept of relationships. In simple English meaning of inheritance is when a child acquired any properties or qualities from a parent then it will be called as an inheritance.

In the same way, relationships can be created between classes.

In classes also you can create a parent and child relationships and child classes can acquire or inherit the properties and methods of a parent class.

Let’s see Inheritance in PHP in details.

Let’s create two classes Apple and Samsung.

Apple Class:

<?php
class Apple
{
 private $color;
 private $appleApp;

 public function setColor($color)
 {
  $this->color = $color;
 }
 public function getColor()
 {
  return $this->color;
 }

 public function setAppleApp($app)
 {
  $this->appleApp = $app;
 }
 public function getAppleApp()
 {
  return $this->appleApp;
 }

}

$iphone = new Apple();
$iphone->setColor('Black');
$iphone->setAppleApp('Apple Store');
echo "My iphone is having ".$iphone->getColor()." color, with ".$iphone->getAppleApp();



class Samsung
{
 private $color;
 private $price;
 private $samsungApp;

 public function setColor($color)
 {
  $this->color = $color;
 }
 public function getColor()
 {
  return $this->color;
 }

 public function setSamsungApp($app)
 {
  $this->samsungApp = $app;
 }
 public function getSamsungApp()
 {
  return $this->samsungApp;
 }

}

$galaxy = new Samsung();
$galaxy->setColor('White');
$galaxy->setSamsungApp('Samsung Store');
echo "<br>";
echo "My galaxy is having ".$galaxy->getColor()." color, with ".$galaxy->getSamsungApp();
?>


The output of the above code will be:

My iphone is having Black color, with Apple Store
My galaxy is having White color, with Samsung Store

If you observe, in both the classes Apple and Samsung some codes are common.

Properties color and its setter and getter are common in both the classes and also performing the same operations of setting and getting the property.

This is a repetition of code. Which is unnecessary.

As both, the classes have some similar things you can group them up in a single parent class and share with both the classes using inheritance.

How to implement Inheritance in PHP?

In order to implement inheritance in classes, you need to create a parent class with the properties and methods common to other classes which we call as child classes.

The child class can inherit the parent class using the “extends” keyword.

Now both parent and child classes can use the code present in the parent class.

Let’s create a parent class “Mobile”, which is having a common property “color” setter and getter methods.

And using extends keyword inherit Mobile class into Apple and Samsung child classes.

<?php

class Mobile
{
 private $color;
 
 public function setColor($color)
 {
  $this->color = $color;
 }
 public function  getColor()
 {
  return $this->color;
 }

}

class Apple extends Mobile
{
 private $appleApp;

 public function setAppleApp($app)
 {
  $this->appleApp = $app;
 }
 public function getAppleApp()
 {
  return $this->appleApp;
 }

}

$iphone = new Apple();
$iphone->setColor('Black');
$iphone->setAppleApp('Apple Store');
echo "My iphone is having ".$iphone->getColor()." color, with ".$iphone->getAppleApp();



class Samsung extends Mobile
{
 private $samsungApp;

 public function setSamsungApp($app)
 {
  $this->samsungApp = $app;
 }
 public function getSamsungApp()
 {
  return $this->samsungApp;
 }

}

$galaxy = new Samsung();
$galaxy->setColor('White');
$galaxy->setSamsungApp('Samsung Store');
echo "<br>";
echo "My galaxy is having ".$galaxy->getColor()." color, with ".$galaxy->getSamsungApp();
?>


The output of the above code will be:

My iphone is having Black color, with Apple Store
My galaxy is having White color, with Samsung Store

As you see the output before inheritance and after inheritance are same.

In inheritance, the code from a parent class which is defined as the public becomes available in a child class also.

So even the code which is not present in a child class, in this case, setter and getter methods for a color property, will become available due to the inheritance of the parent class Mobile.

That’s why even though after creating objects of child classes you can set the color property for a child class objects which is present in parent class only.

You can also check official documentation of PHP for more details on Inheritance.

Types of Inheritance in PHP

There are two types of inheritance in a PHP OOPs

  1. Single level inheritance
  2. Multiple level inheritance

 

Single level inheritance in PHP:

In a single-level inheritance, only one child class extends the parent class members.

Let’s see a simple example:

<?php
class A
{
  private $name;
  
  public function setName($name)
  {
   $this->name = $name;
  }
  public function hello()
  {
   echo "<br>Hello from ".$this->name;
  }
 }
 
 class B extends A
 {
  public function helloFromChild()
  {
   echo "<br>Hello from Class B";
  }
 }
 
 $a = new A();
 $a->setName('Jhon');
 $a->hello();
 
 $b = new B();
 $b->setName('Jenny');
 $b->hello();
 $b->helloFromChild();
?>

The output will be:

Hello from Jhon
Hello from Jenny
Hello from Class B

 

Multilevel inheritance in PHP:

In Multilevel Inheritance, the parent class is inherited by a child class and again that child class will be inherited by another child class, and so on.

Example:

<?php
 class A
 {
  private $name;
  
  public function setName($name)
  {
   $this->name = $name;
  }
  public function hello()
  {
   echo "<br>Hello from ".$this->name;
  }
 }
 
 class B extends A
 {
  public function helloFromChild()
  {
   echo "<br>Hello from Class B";
  }
 }
 class C Extends B
 {
  public function helloFromChild()
  {
   echo "<br>Hello from Class C";
  }
 }
 
 $a = new A();
 $a->setName('Jhon');
 $a->hello();
 
 $b = new B();
 $b->setName('Jenny');
 $b->hello();
 $b->helloFromChild();
 
 $c = new C();
 $c->setName('Vicky');
 $c->hello();
 $c->helloFromChild();
?>

The output of the above program will be:

Hello from Jhon
Hello from Jenny
Hello from Class B
Hello from Vicky
Hello from Class C

In the above program class, A is inherited by class B and class B is inherited by class C. In this way, we can achieve multilevel inheritance.

Also as you notice both class B and class C are having same function i.e. helloFromChild().

But when called by the objects of class B and C, their respective get functions are called. This comes under polymorphism which we will see in later chapters.

The protected access modifier

In this section of inheritance in PHP, you will see what is “protected” access modifier in detail. In the previous tutorial, you have learned about public and private access modifier.

In our previous code, in the parent class Mobile, we have set a property “color” as private. And as per rules, you cannot directly access private properties and methods outside the class.

Hence we have used public setter and getter methods to access the private color property.

But still, if you want to use private members of a parent class into the child class then?

Let’s try it.

Create a function named hello() inside a child class Apple and access a color property directly using $this.

<?php

class Mobile
{
 private $color;
 
 public function setColor($color)
 {
  $this->color = $color;
 }
 public function  getColor()
 {
  return $this->color;
 }

}

class Apple extends Mobile
{
 private $appleApp;

 public function setAppleApp($app)
 {
  $this->appleApp = $app;
 }
 public function getAppleApp()
 {
  return $this->appleApp;
 }
 
 public function hello()
 {
  echo "Hello from ".$this->color." colored mobile";
 }

}

$iphone = new Apple();
$iphone->setColor('Black');
$iphone->setAppleApp('Apple Store');
$iphone->hello();
?>

In the above program, we have created a method “hello” and in this method, we are trying to access a private property declared in parent class Mobile i.e. “color” directly using $this->color.

But this is not allowed and a notice – Notice: Undefined property: Apple::$color” is displayed after calling a method “hello” using $iphone object, of a child class Apple.

And the output of a hello method will be:

Hello from colored mobile

As you see in the above output value of a color property is not display even we have set color using a child $iphone object.

This happens because we are trying to access private property declared in the parent class directly outside the parent class i.e. in the child class.

To overcome this problem “protected” access specifier comes in picture.

The “protected” access modifier allows access of class members within a class itself and into the child or inherited classes.

Now change the access modifier of “color” property in a parent class Mobile from “private” to “protected” and run the same code again.

<?php

class Mobile
{
 protected $color;
 
        //Previous code --------------------------
?>

At this time the notice will not be displayed and you will get an output as:

Hello from Black colored mobile

Similarly, you can also make a method protected in a parent class in order to access it in a child class only.

A protected method in a parent class

Let’s create a child class of an Apple class at this time named IphoneX and make hello function protected in a parent class Apple.

And try to access hello from a child class IphoneX.

<?php

class Mobile
{
 protected $color;
 
 public function setColor($color)
 {
  $this->color = $color;
 }
 public function  getColor()
 {
  return $this->color;
 }

}

class Apple extends Mobile
{
 private $appleApp;

 public function setAppleApp($app)
 {
  $this->appleApp = $app;
 }
 public function getAppleApp()
 {
  return $this->appleApp;
 }
 
 protected function hello()
 {
  echo "Hello from ".$this->color." colored mobile";
 }

}

class IphoneX extends Apple
{
 public function show()
 {
  echo $this->hello()." of IphoneX.";
 }
}

$iphonex = new IphoneX();
$iphonex->setColor('Black');
$iphonex->setAppleApp('Apple Store');

$iphonex->show();
?

In the above program, we have implemented multilevel inheritance.

Class Mobile is extended by the class Apple and Apple class is extended by the class IphoneX.

In a parent class hello method is made protected and its called from a child class IphoneX from a method show.

The output of the above program will be:

Hello from Black colored mobile of IphoneX.

As you see in above program we have called a “show” method from IphoneX object, and from the show method, a protected method hello is called.

In this way, you can use protected access modifier you can access parent class members into the child class and make them inaccessible outside the class.

In this tutorial, we have seen what is inheritance in PHP and also the working of protected access modifier in detail.

In the next chapter, we will see the next feature of an object oriented programming that is Abstraction.


Spread the love

Leave a Comment