Inheritance in PHP OOP

Access Modifiers in PHP
Constants in PHP OOP

What is Inheritance?

When a class derives from another class, it is known as inheritance. Here are some important terms related to inheritance in PHP.

  • Parent Class – The class from which the other classes are derived is called parent class. It is also known as base class.
  • Child Class – The class that is derived from another class is called child class. There are some other names for it also like leaf class or derived class.
Inheritance in PHP

How to inherit a class in PHP?

A class can be inherited by using the extends keyword. Look at the following example.

<?php
class Fruit {
    protected $name;
    protected $color;

    public function __construct($name, $color) {
        $this->name = $name;
        $this->color = $color;
    }

    public function getName() {
        return $this->name;
    }

    public function getColor() {
        return $this->color;
    }
}

class Apple extends Fruit {
    protected $variety;

    public function __construct($name, $color, $variety) {
        parent::__construct($name, $color);
        $this->variety = $variety;
    }

    public function getVariety() {
        return $this->variety;
    }
}

$apple = new Apple('Apple', 'Red', 'Fuji');
echo $apple->getName(); // Output: Apple
echo $apple->getColor(); // Output: Red
echo $apple->getVariety(); // Output: Fuji
?>

In this example, the class Apple extends the class Fruit, which means that the Apple class contains all of the Fruit class’s attributes and methods in addition to its own. The Apple class constructor uses the parent:: construct() function to call the parent constructor, supplying the appropriate parameters. Then, using inheritance, you can create an instance of the Apple class and access the function from the parent class.

To summarize the above:

  1. Inheritance allows the child class to access the public __construct, methods and properties of the parent class.
  2. In the above example, we can access the method of apple class using its object, as well as the methods of parent class.

Scope of protected access modifiers in Inheritance

In the previous tutorial, we learned that protected attributes/methods of a class are accessible within the class and the classes derived from it.

To understand the scope of protected access modifiers in inheritance, let’s look at the following example.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "I am $this->name and my color is $this->color";
  }
}

class Apple extends Fruit {
  public function message() {
    echo "I am from Fruit class or Apple one?";
  }
}

$apple = new Apple("Apple", "red");
$apple->message();
$apple->intro();
?>

In the above example:

  • We make a class called fruit that has certain public characteristics and a protected function.
  • We build another class, apple, which extends from Fruit and has its own instance.
  • When we try to access the protected method of the Fruit class using apple class object, it gives error because, we are trying to access protected member of Fruit class outside the derived class.

Let’s look at another example, which is essentially a little modified version of the last one.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "I am {$this->name} and my color is {$this->color}.";
  }
}

class Apple extends Fruit {
  public function message() {
    echo "Am I a fruit or an apple? ";
    $this -> intro(); // protected
  }
}

$apple = new Apple("Apple", "red");
$strawberry->message();
?>

Because we are accessing it within the class in the preceding example, the protected method of the Fruit class is available in the apple class.

Overriding in Inheritance

Overriding means redefining the parent class’s methods in the child classes with the same name. Consider the following inheritance overriding example.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "I am {$this->name} and my color is {$this->color}.";
  }
}

class Apple extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function intro() {
    echo "I am {$this->name}, my color is {$this->color}. Also, my weight is {$this->weight} kg.";
  }
}

$apple= new Apple("Apple", "red", 50);
$apple->intro();
?>

In the above example:

  • Overriding means renaming the methods of the parent class in the child classes. Consider the following example of overriding in inheritance.
  • It’s worth noting that the __construct and intro methods in the child class apple have the same name. However, the parameters might vary.
  • We create an instance of the apple class and send the arguments to the build method.
  • Because we generated the apple object instance using the apple class constructor, when we call the intro method using an apple instance, it calls the intro function of the apple class.

Role of Final Keyword in inheritance

The final keyword prohibits the child/derived class from overriding the parent/base class’s function. Look at the following example to see how the Final keyword is used.

<?php
class Fruit {
  final public function intro() {
    // code
  }
}

class Apple extends Fruit {
// error to override the intro
  public function intro() {
    // code
  }
}
?>

The apple class attempts to override the intro method in the preceding example. It will throw an error since we used the final keyword with the Fruit class’s intro function.

If you want to read more about PHP inheritance then we recommend the following official guide.

Q&A

Q:In PHP OOP, what is inheritance?
A: In PHP OOP, inheritance is a system that allows a new class to inherit the attributes and methods of a previous class. This implies that the new class can reuse the parent class’s code and functionality while also adding new properties and methods of its own.

Q: In PHP, how do you define a class that inherits from another class?
A: In PHP, the “extends” keyword is used to define a class that inherits from another class. For example, if you have a “ParentClass” parent class and wish to build a “ChildClass” child class that inherits from it, you might write:

class ChildClass extends ParentClass {
  // properties and methods
}

Q: In PHP, can a class inherit from numerous classes?
A: No, in PHP, a class can only inherit from one other class. However, interfaces may be used to provide equivalent functionality, and a class can implement many interfaces.

Q: Is it possible to override a method in a child class?
A: Yes, you may override a method in a child class by creating a method with the same name. When invoked on a child class object, the child class method will be utilized instead of the parent class method.

Q: Is it possible to invoke a parent class method from a child class?
A: Yes, you may use the “parent” keyword followed by the scope resolution operator (::) and the method name to call a parent class method from a child class. As an example:

class ChildClass extends ParentClass {
  public function childMethod() {
    parent::parentMethod();
  }
}

Exercises:

  1. What is the keyword used in PHP to indicate that a class is inheriting from another class?
  2. How can you access a method or property from the parent class in a child class?
  3. How can you call the constructor of the parent class within the constructor of a child class?
  4. How can you prevent a method or property from being overridden in a child class?
  5. What is the difference between the extends and implements keywords in PHP?

Answers:

  1. The extends keyword is used in PHP to indicate that a class is inheriting from another class.
  2. You can access a method or property from the parent class in a child class by using the parent:: keyword, followed by the method or property name.
  3. You can call the constructor of the parent class within the constructor of a child class by using the parent::__construct() method.
  4. You can prevent a method or property from being overridden in a child class by using the final keyword before the method or property declaration in the parent class.
  5. The extends keyword is used to indicate that a class is inheriting from another class, while the implements keyword is used to indicate that a class is implementing an interface.
Access Modifiers in PHP
Constants in PHP OOP
en English
X
Scroll to Top