Backend Development 8 min read

Understanding Constructors in PHP Inheritance

This article explains how PHP constructors work in class inheritance, detailing when child classes must explicitly call parent constructors, how required parameters affect this behavior, and provides clear code examples illustrating proper use of the parent::__construct syntax.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Constructors in PHP Inheritance

Inheritance is a core principle of object‑oriented programming, allowing a subclass to inherit properties and methods from a parent class. In PHP, a constructor is a special method that runs automatically when an object is instantiated.

If a subclass defines its own constructor, it does not automatically invoke the parent constructor unless the parent requires parameters. When the parent constructor has required arguments, the subclass must explicitly call it with those arguments using parent::__construct(...) .

Example of a base class School with a $name property and a constructor that initializes this property:

<?php
class School {
    public string $name;
    public function __construct(string $name) {
        $this->name = $name;
    }
    public function study(): string {
        return "Studying";
    }
}

A subclass OnlineSchool extends School , adds a $platform property, and calls the parent constructor to set the name:

<?php
class OnlineSchool extends School {
    public string $platform;
    public function __construct(string $name, string $platform) {
        parent::__construct($name);
        $this->platform = $platform;
    }
}

The call to parent::__construct($name) is necessary because School 's constructor requires the $name argument. If the parent constructor had no required parameters, PHP would invoke it automatically.

Another subclass PhysicalSchool demonstrates passing both a name and a location to the parent constructor while initializing its own $location property:

<?php
class PhysicalSchool extends School {
    public string $location;
    public function __construct(string $name, string $location) {
        parent::__construct($name);
        $this->location = $location;
    }
}

Running the code shows the expected output, confirming that the parent constructor receives the required parameters and the subclass properties are correctly set:

<?php
require_once 'School.php';
require_once 'OnlineSchool.php';
$onlineSchool = new OnlineSchool("School of Coding", "Udemy");
print $onlineSchool->name;      // School of Coding
print $onlineSchool->platform; // Udemy

require_once 'PhysicalSchool.php';
$physicalSchool = new PhysicalSchool("School of Coding", "New Jersey");
print $physicalSchool->name;     // School of Coding
print $physicalSchool->location; // New Jersey

These examples illustrate the best practice of explicitly calling the parent constructor in a subclass whenever the parent requires arguments, ensuring clear and reliable object initialization.

BackendPHPobject-orientedinheritanceConstructors
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.