Free cookie consent management tool by TermsFeed Generator PHP Inheritance | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
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

undo git merge the easy way and then undo the undoing! (video)
Nov 27, 2022 programming

undo git merge the easy way and then undo the undoing! (video)

we are going to talk about how to undo git merge and then we talk about how to undo the undoing ...

9 Min Read Read More
PHP curl
Mar 22, 2023 programming

PHP curl

Today we are going to talk about curl in PHP. Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP? curl is the answer. ...

6 Min Read Read More
PHP Simple Tinker Like Script
Feb 15, 2023 programming

PHP Simple Tinker Like Script

Today we are going to build a simple Tinker in PHP. Tinker is a tool that allows users to interact with the application through the command line. ...

8 Min Read Read More
Machine Learning in PHP
Mar 18, 2023 programming

Machine Learning in PHP

Machine learning is a way for computers to learn and improve at tasks without being specifically programmed to do so. Are you curious about machine learning and PHP? ...

10 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...