PHP Inheritance
PHP - What is Inheritance?
Inheritance in OOP = Inheritance occurs when one class inherits from another class.
The public and protected properties and methods of the parent class will be passed down to the child class. It may also have its own set of properties and methods.
The extends
keyword is used to specify an inherited class.
Example :- A simple inheritence
A is " . $this->a;
}
}
// aChild is inherited from aParent
class aChild extends aParent {
public $b = 20;
public function printB() {
echo "
B is " . $this->b;
}
}
$cc = new aChild();
$cc->printA();
$cc->printB();
?>
Output :-
B is 20
Example Explained :-
From the aChild class, aParent class is inherited.
Due to inheritance, the aChild class can access the aParent class's
public $a
attribute, and printA()
method.
The aChild class also includes a method called printB()
that can be invoked.
Why We Need Inheritence
The main reason is use inheritence to make data and source code more reusability.
Example 1 :- We have created two normal classes "Add" and "Sub" without inheritence.
a + $this->b;
echo "
Addition = " . $c;
}
}
class Sub
{
public $a, $b;
public function doSub()
{
$c = $this->a - $this->b;
echo "
Subtraction = " . $c;
}
}
?>
In the above example, there is no any relations between both classes.
Now we want to make a relation between these classes. Ho to make it?
First, find common properties and methods from both classes and keep them in a seperate class (It will be parent class).
Next, keep remaining unique properties and methods in their classes ("Add" and "Sub") itself. Now these classes ("Add" and "Sub") are child classes.
And then, make inherit of parent class into child classes.
Example 2:- Making a parent and child class.
a + $this->b;
echo "
Addition = " . $c;
}
}
class Sub extends AB
{
public function doSub()
{
$c = $this->a - $this->b;
echo "
Subtraction = " . $c;
}
}
?>
Note:- In the above programs, the $a
and $b
variables are created only once.
So it makes code reusability.
PHP - Inheritance and the Protected Access Modifier
Protected
properties or methods can be accessible both within and by classes inherited from the class.
Example 1 :- How to access a protected modifier from parent class.
name = $name; // OK, Its public
$this->age = $age; // OK, $age can be accessed in child class
$this->salary = $salary;
}
public function printInfo() {
echo "
Name = " . $this->name;
echo "
Age = " . $this->age;
echo "
Salary = " . $this->salary;
}
}
$cc = new aChild();
$cc->printInfo();
/*
$cc->name = "xy"; // OK, Its public
$cc->age = 23; // ERROR, because it is protected
$cc->salary = 15000; // OK, Its public
*/
$cc->set_Name_Age_Salary("xyz", 34, 18000); // OK, Accessed by a public member from child class
$cc->printInfo();
?>
Output :-
Age = 12
Salary = 20000
Name = xyz
Age = 34
Salary = 18000
Related Links
PHP - Overriding Inherited Methods
Redefining the methods in the child class (with the same name) can be used to override inherited methods.
Example :- Consider the following example. The aChild class (print()
)
methods will override the aParent class print()
method.
Parent Name = " . $this->name;
}
}
// aChild is inherited from aParent
class aChild extends aParent{
public $name = "xy";
public function print() {
echo "
Child Name = " . $this->name;
}
}
$cc = new aChild();
$cc->print();
?>
Output :-
Related Links
PHP - The final Keyword
Using the final
keyword, you may prevent class inheritance or method overriding from happening.
Example 1 :- Class inheritance can be prevented using the following example :
Output :-
Example 2 :- The following example shows how to prevent method overriding :
Output :-