PHP - Overriding Inherited Methods

PHP - Overriding Inherited Methods

Protected properties or methods can be accessible both within the class and by classes descended from it, as we learnt in the previous chapter. Just what does that imply?

Let's consider a situation:

 
 <?php

class Car {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "This is {$this->name} and the color is {$this->color}.";
}
}

class Bmw extends Fruit {
public function message() {
echo "This is BMW";
}
}

# Try to call all three methods from outside class

$bmw = new Bmw("BMW", "red"); # OK. __construct() is public
$bmw->message(); # OK. message() is public
$bmw->intro(); # ERROR. intro() is protected

# Output

"This is BMW"

In the aforementioned example, we can see that we will encounter an error if we attempt to call a protected method (intro()) from outside the class. Public methods are functional!

Lets look below example :


 <?php

class Car {
public $name;
public $color;
public function __construct($name, $color) {
$this
->name = $name;
$this
->color = $color;
}
protected function intro() {
echo "This is {$this->name} and the color is {$this->color}.";
}
}

class Bmw extends Car {

public function message() {

echo "is this BMW or Prado ?";

$this
-> intro(); #Call protected method from within derived class - OK

}

}

$bmw
= new Bmw("BMW", "Black");
# OK. __construct() is public

$bmw->message();
#OK. message() is public and it calls intro() (which is protected) from within the derived class

#Output

"is this BMW or Prado ? The car is BMW and the color is Black."

We can see the above example  that all works fine! It is because we call the protected method (intro()) from inside the derived class.

Inherited methods can be overridden by redefining the methods (use the same name) in the child class.

Look at the example below.

The __construct() and intro() methods in the child class (Bmw) will override the __construct() and intro() methods in the parent class (Car):


 <?php
class Car {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The Car is {$this->name} and the color is {$this->color}.";
}
}

class Bmw extends Car {

public $size;

public function __construct($name, $color, $size) {
$this->name = $name;
$this->color = $color;
$this->size = $size;
}
public function intro() {
echo "The car is {$this->name}, the color is {$this->color}, and the size is {$this->size} .";
}
}

$bmw = new Bmw("BMW", "Black", "Medium");
$bmw->intro();

#output

"The car is BMW, the color is Black, and the size is Medium."


The final keyword is PHP.

In order to prevent method overriding or class inheritance, use the final keyword.


The example that follows demonstrates how to stop class inheritance:


 <?php
final class Car {
# logic
}

# will result in error
class Bmw extends Car {
# logic
}

The following example shows how to prevent method overriding:


 <?php
class Car {
final public function intro() {
# logic
}
}

class Bmw extends Car {

#will result in error

public function intro() {
# logic
}
}

Hope this article will help you to know about Inheritance