CodeBlanks Logo Icon CodeBlanks

Constructor and abstract

PHP course · Intermediate · Lesson 57/115

Learn to define a constructor in an abstract class and call it with parent:: in a subclass.

Constructors in abstract classes

You can define a __construct inside an abstract class. This constructor cannot be called directly with new (because the class is abstract); but it runs when a subclass is instantiated with new. So the constructor provides shared initialization for subclasses.

For example, the Vehicle abstract class assigns the $name property in its constructor. Subclasses do not rewrite this logic; they inherit it.

If the subclass does not define its own constructor, the parent's is used automatically. But if the subclass writes its own constructor, it must call parent::__construct() explicitly.

Call the parent with parent::__construct

If the subclass defines its own constructor, the parent's constructor is not called automatically. You must call it explicitly with parent::__construct(). Otherwise the parent's initialization logic is skipped.

The typical pattern: the subclass constructor takes extra parameters, first calls parent::__construct(), then assigns its own extra properties.

class Car extends Vehicle {
    public int $wheels;
    public function __construct(string $name, int $wheels) {
        parent::__construct($name);
        $this->wheels = $wheels;
    }
}

This pattern preserves shared initialization while adding subclass-specific fields.

Exercise Abstract constructor + parent call

Define a Vehicle abstract class with a public string $name property. Its constructor public function __construct(string $name) should assign $this->name = $name. Then derive Car from Vehicle. First declare public int $wheels in Car. The Car constructor public function __construct(string $name, int $wheels) should first call parent::__construct($name), then assign $this->wheels = $wheels. Finally, a describe() method should do return $this->name . " " . $this->wheels . " wheels";. Use $c = new Car("Toyota", 4) and echo $c->describe() . "\n";

Show example solution
:__construct($name), then $this->wheels = $wheels. describe() returns $this->name . " " . $this->wheels . " wheels".
Example solution:
<?php
// Define the Vehicle abstract class
abstract class Vehicle {
    public string $name;

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

class Car extends Vehicle {
    public int $wheels;

    public function __construct(string $name, int $wheels) {
        parent::__construct($name);
        $this->wheels = $wheels;
    }

    public function describe(): string {
        return $this->name . " " . $this->wheels . " wheels";
    }
}

$c = new Car("Toyota", 4);
echo $c->describe() . "\n";

Test yourself

  1. If you define a constructor in the abstract class, what must the subclass do?

    • Nothing; the parent constructor is called automatically
    • Call parent::__construct() explicitly
    • Cannot redefine the constructor
    • The subclass cannot define a constructor
    Show answer

    Call parent::__construct() explicitly — Correct. When the subclass defines its own constructor, it must call parent::__construct() explicitly.

  2. Parent constructor call

        public function __construct(string $name, int $wheels) {
            ____
            $this->wheels = $wheels;
        }
    Show answer
        public function __construct(string $name, int $wheels) {
            parent::__construct($name);
            $this->wheels = $wheels;
        }
  3. Match the constructor and abstract concepts

    abstract constructor
    Defined in abstract class
    parent::__construct
    Calls the parent constructor
    shared init
    Common setup step
    subclass constructor
    Can take extra parameters

All lessons in Intermediate