PHP - Inheritance

Inheritance is a mechanism of extending an existing class.
By inheriting a class, we create a new class with all functionality of that existing class, and we can add new members to the new class. This way, we can extend existing class without modifying its code.

When we inherit one class from another we say that inherited class is a subclass, because it is created from another class. Term successor can be also used when referring to to the subclass. Class from which we inherit is called a parent class, or predecessor. In our examples we will use terms subclass and parent class.

Inheritance requires that we have at least one existing class. Then, we declare a new class with additional keyword extends. In our example, we have a class person, with members for holding properties of any person - name, address, phone number. We are extending this class and create a class EmployedPerson. We do this because not all persons are employed, so we need a different class to describe them. This class has all properties and functions of class Person, because it is inherited from her. We have just added properties specific to employed persons. We have also changed body of function printPersonInfo, because it should behave differently, now we have more info to display.

Two very important keywords in object model are $this and parent. You may realize that we access class members as

$this->name;

$this is a reference to the calling object and we use it to access class members.

parent is a keyword which we use to access members of a parent class. We access them with:

parent::printPersonInfo();

Because these two exists, we can make a difference between parent class members, and subclass members. You will see in our example that we use

parent::printPersonInfo();

to print basic person info, and then we have three more lines to print extended employed person info. We are first calling parent class function to do its job, and then we add additional code.

As you work with objects, you will find yourself using these keywords very often.

<?php
class Person {
    var $name;
    var $address;
    var $phone;
    function printPersonInfo()
    {
        echo $this->name . "\n";
        echo $this->address . "\n";
        echo $this->phone . "\n";
    }
}
class EmployedPerson extends Person {
    var $ocupation;
    var $company_name;
    var $business_phone;
    function printPersonInfo()
    {
        parent::printPersonInfo();
        echo $this->occupation . "\n";
        echo $this->company_name . "\n";
        echo $this->business_phone . "\n";
    }
}
$kid = new Person();
$kid->name = "Jimmy";
$kid->address = "Elm street";
$kid->phone = "555-5555";
$adult = new EmployedPerson();
$adult->name = "Jimmy's Father";
$adult->address = "Elm street";
$adult->phone = "555-5555";
$adult->occupation = "Programmer";
$adult->company_name = "SoftwareDev Ltd";
$adult->business_phone = "444-4444";
$kid->printPersonInfo();
$adult->printPersonInfo();
?>

Related Marakana Courses

  • PHP and MySQL Bootcamp Training

4 Responses to “PHP - Inheritance”

  1. Harpreet Khera Says:

    I have a question…
    How many types and levels of Inheritance available in PHP?

  2. dinesh Says:

    basically in php only SINGLE inheritance is allowed…BUT in the latest version
    multiLEVEL is also included….also many say that hierarchial also is allowed,,but i havnt tried yet….

  3. dinesh Says:

    just confirm whether hierarchial is allowed or not…please post a reply..

  4. sachin Says:

    what is the easy way to access the method of a class

Leave a Reply