Summary: in this tutorial, you’ll learn how to call the parent constructor from the constructor of the child class.
Child class doesn’t have a constructor
In the inheritance tutorial, you have learned how to define the SavingAccount
class that inherits the BankAccount
class:
However, we haven’t discussed the constructors of the parent and child classes in the context of inheritance.
The following adds a constructor to the BankAccount
class, which accepts the $balance
parameter. The constructor assigns the $balance
argument to the $balance
property:
<?php
class BankAccount
{
private $balance;
public function __construct($balance)
{
$this->balance = $balance;
}
public function getBalance()
{
return $this->balance;
}
public function deposit($amount)
{
if ($amount > 0) {
$this->balance += $amount;
}
return $this;
}
}
Code language: HTML, XML (xml)
The SavingAccount
class remains the same and doesn’t include its own constructor:
<?php
class SavingAccount extends BankAccount
{
private $interestRate;
public function setInterestRate($interestRate)
{
$this->interestRate = $interestRate;
}
public function addInterest()
{
// calculate interest
$interest = $this->interestRate * $this->getBalance();
// deposit interest to the balance
$this->deposit($interest);
}
}
Code language: HTML, XML (xml)
When you create a new instance of the SavingAccount
, PHP will call the constructor of the SavingAccount
class. However, PHP cannot find the constructor in the SavingAccount class
. Therefore, it continues to search for the constructor of the parent class of the SavingAccount
class, which is the BankAccount
class. And it invokes the constructor of the BankAccount
class.
If you don’t pass an argument to the constructor of the SavingAccount
class, you’ll get an error:
$account = new SavingAccount();
Code language: PHP (php)
Error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function BankAccount::__construct(), 0 passed in ... on line 5 and exactly 1 expected in ...
Code language: JavaScript (javascript)
But if you pass an argument to the constructor, it’ll work perfectly:
$account = new SavingAccount(100);
Code language: PHP (php)
Define a constructor in the child class
A child class can have its own constructor. For example, you can add a constructor to the SavingAccount
class as follows:
<?php
class SavingAccount extends BankAccount
{
private $interestRate;
public function __construct($interestRate)
{
$this->interestRate = $interestRate;
}
public function setInterestRate($interestRate)
{
$this->interestRate = $interestRate;
}
public function addInterest()
{
// calculate interest
$interest = $this->interestRate * $this->getBalance();
// deposit interest to the balance
$this->deposit($interest);
}
}
Code language: HTML, XML (xml)
The SavingAccount
class has a constructor that initializes the interestRate
property.
When a child class has its own constructor, the constructor in the child class will not call the constructor of its parent class automatically.
For example, the following creates a new instance of the SavingAccount
class and initializes the $interestRate
property to the value 0.05
$account = new SavingAccount(0.05);
Code language: PHP (php)
To call the constructor of the parent class from the constructor of the child class, you use the parent::__construct(arguments)
syntax.
The following changes the constructor of the SavingAccount
class that accepts two arguments: balance & interest rate. It also calls the constructor of the BankAccount
class to initialize the $balance
property:
<?php
class SavingAccount extends BankAccount
{
private $interestRate;
public function __construct($balance, $interestRate)
{
parent::__construct($balance);
$this->interestRate = $interestRate;
}
public function setInterestRate($interestRate)
{
$this->interestRate = $interestRate;
}
public function addInterest()
{
// calculate interest
$interest = $this->interestRate * $this->getBalance();
// deposit interest to the balance
$this->deposit($interest);
}
}
Code language: HTML, XML (xml)
The syntax for calling the parent constructor is the same as a regular method.
Put it all together:
<?php
class BankAccount
{
private $balance;
public function __construct($balance)
{
$this->balance = $balance;
}
public function getBalance()
{
return $this->balance;
}
public function deposit($amount)
{
if ($amount > 0) {
$this->balance += $amount;
}
return $this;
}
}
class SavingAccount extends BankAccount
{
private $interestRate;
public function __construct($balance, $interestRate)
{
parent::__construct($balance);
$this->interestRate = $interestRate;
}
public function setInterestRate($interestRate)
{
$this->interestRate = $interestRate;
}
public function addInterest()
{
$interest = $this->interestRate * $this->getBalance();
$this->deposit($interest);
}
}
$account = new SavingAccount(100, 0.05);
$account->addInterest();
echo $account->getBalance();
Code language: HTML, XML (xml)
The following class diagram illustrates inheritance between the SavingAccount
and BankAccount
classes:
Summary
- The constructor of the child class doesn’t automatically call the constructor of its parent class.
- Use
parent::__construct(arguments)
to call the parent constructor from the constructor in the child class.