PHP Inheritance

PHP Inheritance

Last Updated on Mar 22, 2023

What is Inheritance?

Inheritance is when a class extends another class. It means the new class has all the properties and methods of the old class plus the things it has on its own

Understanding inheritance is very important because it helps you avoid writing duplicated code and it also helps you have well-structured classes

Let me give an example.

Electric cars are still cars. They can do everything that a car can do plus some new things.

So let’s bring this sentence to code

We have a car and it can drive

Class Car {
    public function drive(){
        echo 'driving';
    }
}

Now let’s build the electric car.

We have an electric car that can drive and also has epcu.

We don’t need to write the drive function again. We can extend the car and get all the functionalities from that.

Class ElectricCar extends Car {
    public function epcu(){
        echo 'I am the Electric Power Control Unit';
    }
}

Now let’s have a tesla

Tesla should have all the functionalities of the ElectricCar so let’s extend that Now tesla can drive and have epcu and also can autoDrive

Class Tesla extends ElectricCar {
    public function autoDrive(){
        echo 'I got this. you can sleep';
    }
}

$tesla = new Tesla();
$tesla->autoDrive();
// I got this. you can sleep
$tesla->epcu();
// I am the Electric Power Control Unit
$tesla->drive();
// driving

That’s awesome right?

One last note.

Do you remember I said I will explain inheritance and access modifiers later?

So let’s check the access modifiers again

Private: only itself

Protected: itself and all the children (the classes that extend this class)

Public: everyone

When a class extends another class. The new class (child) can access all the protected and public functions and properties of the old class (parent) But parent cannot access any of the properties and methods of the new class

$car = new Car();
$car->drive();
// driving
$car->epcu();
// throws an error
// call to undefined method car::epcu()

With extending classes you can have very few lines of code but get access to a lot of functionalities.

<?php
Class Car {
    public function drive(){
        echo 'driving';
    }
}

Class ElectricCar extends Car {
    public function epcu(){
        echo 'I am the Electric Power Control Unit';
    }
}

Class Tesla extends ElectricCar {
    public function autoDrive(){
        echo 'I got this. you can sleep';
    }
}

$tesla = new Tesla();
$tesla->autoDrive();
// I got this. you can sleep
$tesla->epcu();
// I am the Electric Power Control Unit
$tesla->drive();
// driving
https://youtu.be/avNFdwm9L3I

Conclusion

Now you know about inheritance in PHP.

I recommend you to open a PHP files and write multiple classes. make one class extend the other and see what can you access from the parent after extending that class.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • inheritance in PHP
  • access modifiers in inheritance

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Courses