PHP - Inheritance
With inheritance, a child class receives every public and protected property and method of its parent class, and it can add properties and methods of its own.
Note: A child class has no access to the private methods of its parent.
Use the extends keyword to create an inherited class.
In the next example, Strawberry extends Fruit. Thanks to inheritance, it can use the public $name and $color properties and the public __construct() and intro() methods of Fruit. On top of those, Strawberry defines one more method of its own, message().
Example
Strawberry gets everything public from Fruit and adds message():
Output: PHP script
<?php
// Parent class
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is $this->name and the color is $this->color.<br>";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->intro();
$strawberry->message();
?>
PHP - Inheritance and the Protected Access Modifier
The previous chapter showed that protected members are open to the class itself and to classes derived from it.
Outside code is a different story: a call to a protected method from there ends in an error, while public methods keep working normally.
Example
Here, outside code calls the protected method intro() and gets an error:
Output: PHP script
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is $this->name and the color is $this->color.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public
$strawberry->intro(); // ERROR. intro() is protected
?>
Example
The derived class can call the same protected method from inside, with no problem:
Output: PHP script
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is $this->name and the color is $this->color.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
// Call protected method from within derived class - OK
$this -> intro();
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public and calls intro() (which is protected) from within the derived class
?>
PHP - Overriding Inherited Methods
A child class can replace a method it inherits. Just define a method with the same name in the child class; this is called overriding.
In the next example, Strawberry (the child) supplies its own __construct() and intro(), and these override the versions in Fruit (the parent):
Example
The versions of __construct() and intro() in Strawberry are the ones that run:
Output: PHP script
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is $this->name and the color is $this->color.";
}
}
class Strawberry extends Fruit {
public $weight;
public function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "A $this->name is $this->color, and the weight is $this->weight gram.";
}
}
$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
?>
PHP - The final Keyword
Sometimes a class or method should stay exactly as written. The final keyword does that: on a class it blocks inheritance, and on a method it blocks overriding.
Example
A class marked final cannot be extended, so this code ends in a fatal error:
Output: PHP script
<?php
final class Fruit {
// some code
}
// will result in error
class Strawberry extends Fruit {
// some code
}
?>
Example
A method marked final cannot be overridden in a child class, so this code also ends in a fatal error:
Output: PHP script
<?php
class Fruit {
final public function intro() {
// some code
}
}
class Strawberry extends Fruit {
// will result in error
public function intro() {
// some code
}
}
?>