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

Deploy PHP to Heroku
Feb 14, 2023 programming

Deploy PHP to Heroku

Today we are going to talk about deploying your PHP on heroku. Having your code on your local machine is one thing and deploying it on a server and sharing it with the world is another thing. ...

7 Min Read Read More
PHP compact
Feb 14, 2023 programming

PHP compact

Today we are going to talk about compact function in PHP. Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code. ...

4 Min Read Read More
PHP Password Hashing
Mar 22, 2023 programming

PHP Password Hashing

Today we are going to talk about hashing passwords in PHP. Storing and managing passwords is very important. And in PHP it’s very easy. ...

4 Min Read Read More
PHP Array Replace
Feb 14, 2023 programming

PHP Array Replace

Today we are going to talk about array replace function in PHP. Array replace is another array function that is very simple yet a very powerful function. ...

8 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 ...