PHP Inheritance: Concept, Override method, Modify & Final keyword

Here we are going to overview PHP Inheritance, What is Inheritance in PHP is the concept of how to create, modify or override inherited methods and properties.

The Concept of Inheritance in PHP

PHP Inheritance

Inheritance is one of the most important parts of OOP in any given programing language.

Inheritance is similar to real-life where a child inherits properties from their parents and parents’ parent.

In Programming, Inheritance means when a class derives from another class.

Here the class from which we derived is called the Parent class and the class we inherit is the Child class.

The PHP Child Class inherits all the public and protected properties and methods from the parent class.

Child Class could have its own properties and methods and in addition, we can also add modify these inherited methods to suit our needs.

The Class which we want to use as a child class will use extends keyword to import all properties and methods.

Here is the syntax-

// Parent Class
class MyParentClass{
    //Some code
}

// Child Class

class MyChildClass extends MyParentClass{
    //Some Code
}


So using inheritance we can not only import Properties and methods from some class but also add our own methods and properties.

We also have the option to modify those properties and methods as per our needs.

For example, we can change the access modifier (Given it’s either protected or public).

We can override inherited methods too.

How to Change Access Modifier in Child Class

Both protected and public methods and properties can be accessed from the child class.

Since you can modify these methods inside your child’s class you can also change the access modifier of any public method to a protected or protected method to the public.

To do this you simply have to mention desired access before the function keyword.

Here is How it would work:

class Smalltalk{
    protected function saysomething{
        echo "You look gorgeous";
    }
}

// Changes in child class

class Greeting extends Smalltalk{
    public function saysomething{
        "Hello There!"
    }
}


How to Override Inherited methods

The whole point of inheriting some class is that we want to get all of its properties and methods.

But that’s not true for each and every case.

If we are creating a child class it means that we are creating a class that performs the same operation with some new things and in a little different way.

If we want to modify a method that we inherit from parent to child class we do it easily.

We just have to place the modification in the child class.

class Smartphone{
    public $model;
    public $price;
    public $brand;
    
    public function __construct($model, $price, $brand){
        $this->name= $model;
        $this->price= $price;
        $this->brand= $brand;
        
    }
    
    public function info(){
        echo "This phone is {$this->model} by {$this->brand}, and its price is {$this->price}"
    }
}

class Android extends Smartphone{
    public $version;
    
    public function __construct($model, $price, $brand, $version){
        $this->name= $model;
        $this->price= $price;
        $this->brand= $brand;
        $this->version= $version;
    }
    
    public function info(){
        echo "This phone is {$this->model} by {$this->brand} cost {$this->price} USD and running on Android {$this->version}"
    }
    
}

That’s how we can modify/Override methods that we inherited in PHP.

How to prevent class inheritance using the final keyword

If you created a class that you really don’t want to use as the parent class you can easily prevent class inheritance for it.

This is done by adding final keywords before the class keywords.

This keyword prevents class inheritance and method overriding.

Here is how it would work:

// Parent Class
final class MyParentClass{
    //Some code
}

// Child Class

class MyChildClass extends MyParentClass{
    //Some Code
}