What does inheritance mean in PHP?
In OOP, inheritance is when a class is based on another class.
All of the public and protected properties and methods of the parent class will be passed on to the child class. It can also have its own properties and ways of doing things.
The extends keyword is used to define a class that is based on another class.
Here’s a good example:
<!DOCTYPE html>
<html>
<body><?php
class Student {
public $name;
public $rollno;
public function __construct($name, $rollno) {
$this->name = $name;
$this->rollno = $rollno;
}
public function intro() {
echo “The student is {$this->name} and the rollno is {$this->rollno}.”;
}
}// Monitor is inherited from Student
class Monitor extends Student {
public function message() {
echo “Am I a student or a Monitor? “;
}
}$monitor = new Monitor(“Ram”, “1001”);
$monitor->message();
$monitor->intro();
?></body>
</html>
Output
Am I a student or a Monitor? The student is Ram and the rollno is 1001.
PHP: The Protected Access Modifier and Inheritance
In the last chapter, we learned that properties or methods that are “protected” can be used both within the class and by classes that are “derived” from that class. What’s the meaning of that?
Here’s a good example:
<!DOCTYPE html>
<html>
<body><?php
class Student {
public $name;
public $rollno;
public function __construct($name, $rollno) {
$this->name = $name;
$this->rollno = $rollno;
}
protected function intro() {
echo “The student is {$this->name} and the rollno is {$this->rollno}.”;
}
}class Monitor extends Student {
public function message() {
echo “Am I a student or a Monitor? “;
}
}// Try to call all three methods from outside class
$monitor = new Monitor(“Ram”, “1001”); // OK. __construct() is public
$monitor->message(); // OK. message() is public
$monitor->intro(); // ERROR. intro() is protected
?>
Output
Am I a student or a Monitor?
In the example above, we can see that we will get an error if we try to call a protected method (intro()) from outside the class. public methods will work perfectly well!
Here’s a second example:
<!DOCTYPE html>
<html>
<body><?php
class Student {
public $name;
public $rollno;
public function __construct($name, $rollno) {
$this->name = $name;
$this->rollno = $rollno;
}
protected function intro() {
echo “The student is {$this->name} and the rollno is {$this->rollno}.”;
}
}class Monitor extends Student {
public function message() {
echo “Am I a student or a monitor? “;
// Call protected function from within derived class – OK
$this -> intro();
}
}
$monitor = new Monitor(“Ram”, “1001”); // OK. __construct() is public
$monitor->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
?></body>
</html>
Output
Am I a student or a monitor? The student is Ram and the rollno is 1001.
In the above example, we can see that everything is fine! Because we call the protected method (intro()) from inside the derived class.
PHP: Overriding Inherited Methods
Inherited methods can be changed by redefining them in the child class and giving them the same name.
Check out the example that follows. The __construct() and intro() methods in the Strawberry class will replace the corresponding methods in the Student class:
Example
<!DOCTYPE html>
<html>
<body><?php
class Student {
public $name;
public $rollno;
public function __construct($name, $rollno) {
$this->name = $name;
$this->rollno = $rollno;
}
public function intro() {
echo “The student is {$this->name} and the rollno is {$this->rollno}.”;
}
}class Monitor extends Student {
public $weight;
public function __construct($name, $rollno, $weight) {
$this->name = $name;
$this->rollno = $rollno;
$this->weight = $weight;
}
public function intro() {
echo “The student is {$this->name}, the rollno is {$this->rollno}, and the weight is {$this->weight} KG.”;
}
}$monitor = new Monitor(“Ram”, “1001”, 50);
$monitor->intro();
?></body>
</html>
Output
The student is Ram, the rollno is 1001, and the weight is 50 KG.
The final Keyword is PHP.
You can use the final keyword to stop a class from inheriting another class or to stop a method from being overridden.
The code below shows how to stop class inheritance from happening:
Example
<!DOCTYPE html>
<html>
<body><?php
final class Student {
}class Monitor extends Student {
}?>
</body>
</html>
Output
PHP Fatal error: Class Monitor may not inherit from final class (Student) in /home/coderazaa/inheritance.php on line 10
Method overriding can be stopped by doing what’s shown in the next example:
Example
<!DOCTYPE html>
<html>
<body><?php
class Student {
final public function intro() {
}
}class Monitor extends Student {
// will result in error
public function intro() {
}
}
?></body>
</html>
Output
PHP Fatal error: Cannot override final method Student::intro() in /home/coderazaa/inheritance.php on line 15
In Next chapter we learn PHP Constants.