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

PHP Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
PHP array filter
Feb 14, 2023 programming

PHP array filter

Today we are going to talk about array filter in PHP. array filter is a very useful function and it helps you filter an array by key, value or both. ...

6 Min Read Read More
PHP Array Sort
Feb 14, 2023 programming

PHP Array Sort

Today we are going to talk about sorting arrays in PHP. we will take a look at some of the most useful and common array sort functions in PHP and compare their functionality. ...

7 Min Read Read More
PHP Send SMTP Emails with PHPMailer
Mar 22, 2023 programming

PHP Send SMTP Emails with PHPMailer

Today we are going to send emails with the help of PHPMailer package. Sending emails is a very useful feature and in PHP it is very easy and powerful. ...

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