Header Ads

Object-Oriented PHP: Inheritance


Prior to this lesson, we've learned about Encapsulation. In this lesson we will be talking about inheritance. Inheritance is fairly easy concept to understand. It's like in real world that children inherit some of their attributes and behavior from their parents.

Let's use the parent and child analogy in this example:
<?php

class Mother
{
    public function getHairColor()
    {
        return 'black';
    }
}

class Child extends Mother
{

}

$child = new Child;

echo $child->getHairColor();
As you can see, this is inheritance in action because the Child class can access the getHairColor method of the Mother class using the extends keyword. If you encounter a class that uses the extends keyword, that means that class can inherit any public or protected methods from the class it extends. Please take note that only public and protected methods or properties can be inherited, because as what we've discussed in our encapsulation lesson private properties or methods can only be accessed within the class that owns it.

When to use inheritance?

Now that we know the basic of inheritance, what are the other scenario that inheritance can be useful? Okay, let's have another example. Let's say we have a Rectangle class that extends the Shape class:
<?php

class Shape
{
    protected $length = 5;
    protected $width = 10;   

    public function getArea()
    {
        return $this->length * $this->width;
    }
}

class Rectangle extends Shape
{

}

$rectangle = new Rectangle;

echo $rectangle->getArea();
We know in math that in order to get the area of a rectangle we have to multiply its length by its width. So, in this example there is no problem using the getArea method from the parent class.

However, when it comes to triangle the formula will be a little bit different. How are we going to solve that? Well, we are going to simply overide the method from the parent class. One of the important concepts of inheritance is that if you need to overide functionality in your parent class then simply overide it, because anything in the child class will take precedence over the parent class.
<?php

class Shape
{
    protected $length = 5;
    protected $width = 10;   

    public function getArea()
    {
        return $this->length * $this->width;
    }
}

class Triangle extends Shape
{
    protected $base = 10;
    protected $height = 12;    

    public function getArea()
    {
        return $this->base * $this->height / 2;
    }
}

$triangle = new Triangle;

echo $triangle->getArea();

Now, maybe you're thinking that our Shape class in useless because we can't reuse the getArea method. Well, in this case yes. But if you find your self repeating functionality or attributes in every classes that you working, it's a sign that you have to use inheritance.

If we're going to take this a little bit further, we can use the Shape class as a contract. What I mean by contract is that every time you create a shape, like for example a circle, that class must have a getArea method. We can use an abstract class for that.

An abstract class is a class that you can add functionality but cannot be instantiated.
<?php

abstract class Shape
{
    abstract protected function getArea();
}

class Circle extends Shape
{
    
}

$circle = new Circle;

When we run this code we should get a fatal error.
Fatal error: Class Circle contains 1 abstract method and must therefore be declared
abstract or implement the remaining methods (Shape::getArea)
in D:\Tutorials\Laragle\PHP\OOP\Inheritance.php on line 11

To fix that we have to create a getArea method in our Circle class.
<?php

abstract class Shape
{
    abstract protected function getArea();
}

class Circle extends Shape
{
    protected $radius = 5;

    public function getArea()
    {
        return M_PI * pow($this->radius, 2);
    }
}

$circle = new Circle;

echo $circle->getArea();

That's all for this lesson. I hope that this lesson helps you understand a little bit about inheritance. If you have some questions please don't hesitate to comment it down below. See you on the next lesson.

Previous: EncapsulationNext: Interfaces


No comments