![]() |
Tutorial PHP Object Inheritance - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: Tutorial PHP Object Inheritance (/Thread-Tutorial-PHP-Object-Inheritance) |
PHP Object Inheritance - sunjester - 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. ![]() 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. Code: class Vehicle 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. Code: $car = new Car(); Overloading You can add your own method in the child class to overload the original method in the base class. Code: class Car extends Vehicle References
RE: PHP Object Inheritance - mothered - 12-29-2018 Simple, yet well detailed guide. I don't come across too many tutorials referencing classes. Good work. RE: PHP Object Inheritance - darkninja1980 - 01-30-2019 great work on your tutorial. ![]() RE: PHP Object Inheritance - f0rget - 03-04-2023 good work thanks mate !! RE: PHP Object Inheritance - sensitive - 03-26-2023 great work on your tutorial. |