Skip to content
doubtly

1 answer

Best answer

Answer: Inside the child class constructor, call parent::__construct().

class ParentClass {
    public function __construct($value) {
        echo "Parent got $value\n";
    }
}
class ChildClass extends ParentClass {
    public function __construct($value) {
        parent::__construct($value);  // call parent constructor
        echo "Child constructor\n";
    }
}
new ChildClass(5);

Output: Parent got 5 then Child constructor.

In PHP, if a child class defines its own __construct(), the parent's constructor is not called automatically, so you must call it yourself with the keyword parent (lower case) and the scope operator ::. If the child has no constructor, the parent's constructor is inherited and runs automatically.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.