Categories
PHP

OOP Inheritance

One of the powerful concepts in object-oriented programming is inheritance. Inheritance allows a new class to be defined by extending the capabilities of an existing base class. PHP allows a new class to be created by extending an existing class with the extends keyword.

  1. Overriding Inherited Methods
  2. Using final keyword to prevent class inheritance and method overriding

An inherited class (also called child class or subclass) can inherit all the features of its parent or superclass, adapt some of them, override existing methods and properties, and add new ones of its own.

<?php
 class Human {
  protected $name;
 
  public function __construct($name){
   $this->name = $name;
  }
 }

In order to declare that one class inherits the code from another class, we use the extends keyword:

<?php
 class Person extends Human {
  public function getName(){
   return $this->name; 
  }
 }

The above code demonstrated how the Person class inherited the parent constructor and added a new method getName() that returns the name property defined in the parent class. See the full code:

<?php
 class Human {
  protected $name; 
  public function __construct($name){
   $this->name = $name;
  }
 }

 class Person extends Human {
  public function getName(){
   return $this->name; 
  }
 }
 $person = new Person('xyz');
 echo $person->getName(); # Prints: xyz

The power of inheritance doesn’t come from simply reusing code. Objects created from the extended class can be used as if they were created from the existing base class.

PHP Inheritance and Visibility Modifiers

Member variables or methods on the base class that are declared public or protected are fully available to any inheriting classes, while properties or methods that are declared as private are not. The one exception to this is if a member function on my base class is protected or public in that case, you can call it from the inheriting class. The function would then be able to access any private properties or methods:

<?php
 class ABC {
  private $abc = 'abc';
  protected function not_private() {
    return $abc;
  }
 }

 class DEF extends ABC {
  protected function call_me() {
    // when you call this function (which is on the base class)
    // it will have no problems accessing $abc.
    $this->not_private();
  }
 }

For detail, read Access or Visibility Modifiers in PHP tutorial.

PHP – Overriding Inherited Methods

One of the things you would like the new child class to have is an updated constructor that takes in the age as a parameter. The act of creating a “new” version of a member function (method) in an inheriting class is often referred to as overriding a method. The overridden method in the base class is replaced by the new one in the extending class.

<?php
 class Human {
  protected $name; 
  public function __construct($name){
   $this->name = $name;
  }
 }

 class Person extends Human {
  public $age;
  public function __construct($name, $age){
   $this->name = $name;
   $this->age = $age;
  }
  public function getName(){
   return $this->name; 
  }
 }
 $person = new Person('xyz', 18);
 echo $person->getName(); # Prints: xyz
 echo $person->age; # Prints: 18

You can continue to use the base class’s own constructor by using a keyword called parent. This keyword, combined with the scope resolution operator (::), lets us call methods belonging to my base class:

<?php
 class Person extends Human {
  public $age;
  public function __construct($name, $age){
   $this->age = $age;
   parent::__construct($name); # calls the parent constructor
  }
  public function getName(){
   return $this->name; 
  }
 }

By doing this, you do not have to duplicate the work done by the parent’s constructor. More importantly, you do not have to worry about the parent class’s implementation changing and your diverging you have taken code reuse to a whole new level.

You can similarly override any method by creating its new version in the child class.

PHP final keyword – Prevent class inheritance and method overriding

By prefixing a method with the final keyword, you can prevent inheriting classes from overriding it and implementing their own version.

<?php
 class Human {
  protected $name; 
  final public function setName($name){
   $this->name = $name;
  }
 }

 class Person extends Human {
  public function setName($name){
   $this->name = $name;
  }
 }
 # Fatal error: Cannot override final method Human::setName()

Inheriting classes can still access and call these methods; they just cannot include them in the set of things they extend, modify, or re-implement.

Classes can also be declared as final, which means that nobody is allowed to inherit from them:

<?php
 final class Human {
  // ...
 }

 class Person extends Human {
  // ...
 }
 # Fatal error: Class Person may not inherit from final class (Human)

PHP OOP Tutorials: