Parent Constructor

A Parent Constructor is used to add new functionality in a child's constructor whilst also using the Parent's constructor. Thus both the original Parent constructor and the Child constructor are called. This is achieved by using the keyword parent before the scope resolution operator :: and the __construct() magic method, within the child constructor.

 

Syntax:

parent::__construct();

 

This example shows a ParentClass and two child classes, one of which utilises the parent constructor as well as its own constructor:

Save & refresh browser:

ParentClass object instantiated using the ParentClass constructor
RedChild object instantiated using the ParentClass constructor and the RedChild constructor
BlueChild object instantiated using the ParentClass constructor

Leave a Reply