PHP Inheritance


Inheritance allows child classes to use its parent class members. This is very useful when you want to have common functionalities for most of the child classes in the base class.

Inheritance in PHP:

 

We are going to create a base class called “greetings” which will have a “check_date()” method to check the passed date against current date and “display_greetings()” method with a default greeting text.

Now, we are going to create a child class “newyeargreetings” by inheriting the base class “greetings”. We can print our “Happy New Year” greeting text inside the “display_greetings()” method of our new child class “newyeargreetings”.

 

In the same way we can create any number of greetings child classes.

 

Tags: ,

You may also like