PHP
Accessing parent with parent::
Learn to access the parent's methods and constructor with parent::.
Accessing the parent with parent::
Sometimes the child class wants to call a method of the parent class (especially __construct). PHP uses the parent:: operator for this.
parent:: is a shortcut for writing the parent class name. parent::method() calls the parent's method(). This is critical when the child class defines its own constructor: the parent's constructor is not called automatically, you must write parent::__construct(...) explicitly.
The double colon (::) is called the scope resolution operator.
A parent::__construct example
The Car class should hold both brand and doors. The Vehicle parent constructor expects only brand.
The child constructor takes two parameters: $brand and $doors. It calls parent::__construct($brand) to delegate to the parent's constructor, then initializes its own property with $this->doors = $doors.
This pattern is the standard way to reuse the parent's initialization logic without duplicating it.
Exercise Car constructor calls parent::__construct
In this lesson the Vehicle class takes only brand. You will derive Car from Vehicle and give its constructor two parameters ($brand and $doors). Inside the constructor call parent::__construct($brand) to invoke the parent's constructor, then initialize the doors property with $this->doors = $doors. The info() method should combine brand and doors. Create a Volvo Car with 4 doors and print the info() output.
Show example solution
:__construct($brand), then assigns $this->doors.
Example solution:
<?php
class Vehicle {
public string $brand;
public function __construct(string $brand) {
$this->brand = $brand;
}
}
class Car extends Vehicle {
public int $doors;
public function __construct(string $brand, int $doors) {
parent::__construct($brand);
$this->doors = $doors;
}
public function info(): string {
return $this->brand . " - " . $this->doors . " doors";
}
}
$car = new Car("Volvo", 4);
echo $car->info();
Test yourself
-
What happens when a child class defines its own constructor?
- The parent's __construct is called automatically
- The parent's __construct is not called; parent::__construct() is required
- The class cannot be instantiated
- The parent class is deleted
Show answer
The parent's __construct is not called; parent::__construct() is required — Correct; if the child defines __construct, the parent's is not called automatically, you must write parent::__construct().
-
Complete the parent::__construct call
public function __construct(string $brand, int $doors) {
____::__construct($brand);
$this->doors = $doors;
}
Show answer
public function __construct(string $brand, int $doors) {
parent::__construct($brand);
$this->doors = $doors;
}
-
Match concepts related to parent::
- parent::
- Parent access operator
- ::
- Scope resolution operator
- parent::__construct()
- Calls parent's constructor
- Own __construct
- Parent constructor not auto-called
Next lesson: Method override