0Day Forums
PHP Object Inheritance - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: PHP (https://zeroday.vip/Forum-PHP)
+--- Thread: PHP Object Inheritance (/Thread-PHP-Object-Inheritance)



PHP Object Inheritance - tierell99 - 12-28-2018

Introduction
In this article we will be creating objects and demonstrating inheritance. If you do not know how to create a class in PHP then this tutorial may not be for you. The base class is sometimes called the super class. In PHP the extend keyword is used to inherit from another class. You can also inherit from a class that is being inherited from another class.

UML
Showing object inheritance in UML is pretty simple, its a line from the base class to the child class, the arrow is an outline and not solid, as pictured below. The Car class is inheriting the base class, Vehicle.

[Image: 85sqEhx.png]

Classes
Our classes will be based on vehicles. The base class will be Vehicle. We can create other classes and inherit the methods/functions of the base class. The above UML is converted into PHP (below) to show how the inheritance works.

Hidden Content

The Car class above will automatically have a color variable/attribute. You can use the changeColor method from the base class to set the color of the Car class.

Hidden Content

Overloading
You can add your own method in the child class to overload the original method in the base class.

Hidden Content

References
  • [To see links please register here]

  • [To see links please register here]

  • [To see links please register here]

  • [To see links please register here]




RE: PHP Object Inheritance - labourers546319 - 12-29-2018

Simple, yet well detailed guide.

I don't come across too many tutorials referencing classes.
Good work.


RE: PHP Object Inheritance - Drbartholomeo10 - 01-30-2019

great work on your tutorial.


RE: PHP Object Inheritance - jonietiyrttz - 03-04-2023

good work thanks mate !!


RE: PHP Object Inheritance - anandqtwovpqcr - 03-26-2023

great work on your tutorial.