Inheritance :

Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class. These inherited attributes and behaviors are usually modified by means of extension.
To inherit in PHP5, you should use the keyword 'extends' in the class definition.
Note :In PHP5 only single inheritance is allowed.

example:
    <?php
    class Person {
	    private $name;
	    private $address;
	    public function getName() {
		    return $this->name;
	    }
    }
    class Customer extends Person {
	    private $customer_id;
	    private $record_date;
	    public getCustomerId() {
		    return $this->customer_id;
	    }
	    public getCustomerName() {
		    return $this->getName();// getName() is in Person
	    }
    }
    ?>
Description :

In the above example, class Customer extends from the Person class. This means that Customer class is the child class and the Person is base class. The child class Customer extends the method getName() and calls it in the getCustomerName() method of the Customer class.

Questions for Inheritance

  1. What is Inheritance in PHP? Explain with example?
After editing Click here:
Output:
Hello World