Inheritance in PHP with Example

Mohasin Hossain
2 min readSep 26, 2023

--

Today we going to explore what is Inheritance in PHP and how to use it.

Few things we need to know about Inheritance.

Inheritance allows one class to inherit the traits and behavior of another class. This should instantly make sense, in the same way that a child inherits characteristics from their parents.

Inheritance is the ability to build new classes on top of existing ones.

The main benefit of inheritance is code reuse.

It’s important to strike a balance and choose inheritance when it genuinely represents an “is-a” relationship between classes. For example Dog is a animal so Dog can inherit form Animal.

Let’s see some basic code example

class CoffeeMaker
{
public function brew()
{
var_dump('Brewing the coffee');
}
}

// SpecialtyCoffeeMaker is a CoffeeMaker;
class SpecialtyCoffeeMaker extends CoffeeMaker
{
public function brewLatte()
{
var_dump('Brewing a latte');
}
}

(new SpecialtyCoffeeMaker())->brewLatte();

Let’s see some real life code example

class Product {
protected $name;
protected $price;

public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}

public function getDescription() {
return "$this->name: $this->price";
}
}

class Electronics extends Product {
private $warranty;

public function __construct($name, $price, $warranty) {
parent::__construct($name, $price);
$this->warranty = $warranty;
}

public function getDescription() {
return parent::getDescription() . " (Warranty: $this->warranty years)";
}
}

class Clothing extends Product {
private $size;

public function __construct($name, $price, $size) {
parent::__construct($name, $price);
$this->size = $size;
}

public function getDescription() {
return parent::getDescription() . " (Size: $this->size)";
}
}

// Create instances of products
$phone = new Electronics('Smartphone', 599.99, 2);
$shirt = new Clothing('T-shirt', 19.99, 'M');

// Display product descriptions
echo $phone->getDescription() . PHP_EOL;
echo $shirt->getDescription() . PHP_EOL;

While inheritance offers numerous benefits, it’s essential to use it judiciously and consider other OOP principles like composition and interfaces when designing your classes. Overuse of inheritance can lead to tightly coupled code and can make the system more complex and harder to maintain. Therefore, it’s important to strike a balance and choose inheritance when it genuinely represents an “is-a” relationship between classes.

For more details of OOP (Object Ordinated Programming) , you may have a look at the link given below. Feel free to leave a comment if you have any feedback or questions.

https://github.com/mohasin-dev/Object-Oriented-Programming-in-PHP

Bonus: You must love my most read article : How To Use Single Responsibility Principle in PHP/Laravel
And most GitHub star repo:
https://github.com/mohasinhossain/SOLID-Principles-in-PHP

--

--

Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux