When you need classes with similar variables and functions to another existing class. The extended or derived class has all variables and functions of the base class,and what you add in the extended definition. It is not possible to subtract from a class. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword ‘extends’.

This is also called a “parent-child” relationship. You can create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class.

Example:
Code:

 
class person
{

var $age;
function set_age($age)
{

$this->age=$age;
}
function  get_age()
{
 echo "The age is : " , $this->age ,"
"; } } class student extends person { var $name; function set_name($name) { $this->name=$name; } function get_name() { echo "The name is : " , $this->name; } } $obj=new student; $obj->set_age(23); $obj->get_age(); $obj->set_name("Andy"); $obj->get_name();

Output:

The age is : 23
The name is : Andy

Explanation:

This student that has all variables and functions of person plus an additional variable $name and an additional function set_name() and get_name(). You create a student the usual way and can now set and get the person age. You can still use student functions on student.

eXTReMe Tracker