This simple tutorial is for showing how class inheritance works in PHP and also provide a simple Object Oriented Programming (OOP) concept.
To put things simply, class inheritance means a class can be inherited by a child class. A child class would inherit all the variables & functions the parent class has. Plus a child class can have its own specific variables & its own functions, it can overwrite the parent class’ function as well.
For example, you can have a parent class called “PEOPLE”, then you can have child class that is inherited called “MAN”, or “WOMAN”. Since man & woman is both type of people.
In the following simple example, I define a child class inherit from class “contact_info” in the previous tutorial. The child class called “business_contact_info” inherits all the variables & functions from contact_info, it also has a variable “company” defined which is only unique to child class “business_contact_info”, this child class overwrites the print_contact_info() function (now prints also the company name).
In the example, I also show how to call the parent class’ functions in a child class. For example, I overwrite the constructor function of class “contact_info” with specific constructor for the child class “business_contact_info”. In the “business_contact_info” constructor function, I call the constructor function of “contact_info” to assign the $name variable.
To call a function defined in parent class. Simply use the syntax [class_name]::[function_name] ([parameters]).
The file: “business_contact_info.php“:
//need to include parent class to define the parent class include "contact_info.php"; //To inherit from a class, simply add extends <class name> /* a child class would automatically inherits all the class variables & class functions defined in the parent class. Therefore, business_contact_info has all the class variables & functions in the parent class contact_info */ class business_contact_info extends contact_info { //additional class variables only in this class var $company; var $title; /*Ovewriting the constructor function To set a title for when creating a new business_contact */ public function business_contact_info($name, $title) { /* name is pass along over to the parent class contact_class then the title is set in the business_contact_info class. To call a parent class's function, just use: [Parent ClassName]::[Parent class Function Name] (whatever parameter) */ contact_info::contact_info($name); $this->title = $title; } public function set_company($company) { $this->company = $company; } /* To prevent a child class from overwriting a parent class's function simply put "final" in the parent's function definition For example: to prevent overwrite of function print_contact_info() in parent class contact_info. define the function in parent class contact_info as: final function print_contact_info() ... */ /* overwriting a function */ /* in PHP, to overwrite a function that is defined in parent class. simply define a function with the same name in the child class. */ public function print_contact_info() { $output = "Name is: ". $this->name ."<BR>". "Phone Number is: ". $this->phone_number. "<BR>". "Company name: ".$this->company. "<BR>". "Title: ".$this->title. "<BR>". "Note: ".$this->note; return $output; } } //end of class |
Here is the parent class contact_info.php:
class contact_info { /* declare some class properties as variables */ var $name; var $phone_number; var $note; /* Constructor is simply a function with same name as the class */ public function contact_info($name) { $this->name = $name; } /* To assign a value to the class variable - use $this-> */ /* NOTE: the variablename when use in $this->, does NOT use the $ in front of variable name */ public function set_phone_number($phone_number) { $this->phone_number = $phone_number; } public function set_note($note) { $this->note = $note; } /* function print out the contact detail information by appending the value of class variables */ public function print_contact_info() { $output = "Name is: ". $this->name ." ". "Phone Number is: ". $this->phone_number. " ". "Note: ".$this->note; return $output; } } //end of class |
The file “test_business_contact_info.php” shows how to use the inherited class:
include "business_contact_info.php"; $my_biz_contact = new business_contact_info("Jason", "Software Engineer"); $my_biz_contact->set_phone_number("888-999-1111"); $my_biz_contact->set_note("Jason is a great coder and designer."); $my_biz_contact->set_company("MonkeyCanCode.com"); echo $my_biz_contact->print_contact_info(); |
The output of the code:
Name is: Jason
Phone Number is: 888-999-1111
Company: MonkeyCanCode.com
Title: Software Engineer
Note: Jason is a great coder and designer.
Download all the source code files here: inherit_class_example