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 Read and Write to Files
Mar 21, 2023 programming

PHP Read and Write to Files

Today we are going to learn about working with files in PHP.There are many functions that can help you read the contents of a file. we are going through the most common functions. ...

11 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 Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More
PHP Testing with PHPUnit and Pest
Mar 22, 2023 programming

PHP Testing with PHPUnit and Pest

Today we are going to write tests with PHPUnit and Pest in PHP. Testing is a very important step in developing an application. phpunit and pest make it really easy to write tests in php ...

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