Menu

PHP Constructor: __construct and Property Promotion

A PHP constructor is the __construct() method, which runs automatically when you write new ClassName(...). Learn constructor property promotion, default and named arguments, validation, parent::__construct(), and how to fake multiple constructors with static factory methods.

This page includes runnable editors - edit, run, and see output instantly.

A PHP constructor is a method named __construct(). PHP calls it automatically when you write new ClassName(...), and the arguments in the parentheses become its parameters, so the object starts life with its values already set.

Without a constructor you would create an empty object and set each property afterwards, and nothing would stop you from forgetting one. With a constructor, new User() without arguments is an ArgumentCountError.

Constructor property promotion

Declaring a property, listing it as a parameter and assigning $this->name = $name repeats each name three times. Since PHP 8.0, a visibility keyword in front of a constructor parameter does all three at once:

A promoted parameter can also be readonly (public readonly string $id), which is the shortest way to write an immutable value object. You can mix promoted and normal parameters, and the constructor body can still contain code that runs after the promoted properties are assigned.

Default values and named arguments

Constructor parameters take defaults like any function. Parameters with defaults go after the required ones. With named arguments (PHP 8.0) the caller can skip to the option it wants instead of repeating every default before it:

Run it: the output contains HTML, so the Page tab shows the three buttons rendered, and the Output tab shows the tags PHP printed. Note the parentheses in (new Button('Save'))->render(): since PHP 8.4 they are optional, but PHP 8.3 needs them.

Validate arguments in the constructor

A constructor cannot return false or an error code: whatever it returns is ignored. To refuse bad input, throw an exception. The object is then never created, so the rest of the program can trust every Temperature it receives.

The try catch page covers catching these exceptions in more detail.

Multiple constructors with static factory methods

PHP has no constructor overloading: one class, one __construct. When an object can be built from different inputs (a date string, an array, a timestamp), add named static methods that prepare the arguments and call new static(...). Making the constructor private forces everyone through the factories.

new Color(1, 2, 3) from outside the class now throws an Error ("Call to private Color::__construct() from global scope"). The static page explains why new static is used here rather than new self.

Call the parent constructor with parent::__construct

When a child class defines its own constructor, the parent's constructor does not run by itself. Call it with parent::__construct(...), usually first, so the parent's properties are set before the child adds its own:

If the child has no constructor at all, PHP uses the parent's, and new Truck('Volvo') would pass its arguments straight to Vehicle::__construct. More on this in inheritance.

Forgetting parent::__construct

This mistake produces no error at the line where it happens. The child constructor runs, the parent's typed properties stay uninitialized, and the program fails later, wherever the first read happens:

Add parent::__construct(); as the first line of Post::__construct and run it again: it prints the date. A related method, __destruct(), runs when the last reference to an object goes away or the script ends. It is rarely needed in everyday code, because PHP closes files and frees memory by itself at the end of each request.

Frequently Asked Questions

What is __construct in PHP?

__construct() is the constructor: a method PHP calls automatically right after new creates the object. The arguments you pass to new User('Ada', 36) are passed to __construct(string $name, int $age), which usually stores them in properties.

What is constructor property promotion in PHP?

Since PHP 8.0, putting a visibility keyword on a constructor parameter declares the property and assigns it in one step: public function __construct(private string $name) {} is the same as declaring private string $name; and writing $this->name = $name; in the body.

Can a PHP class have multiple constructors?

No. PHP has no method overloading, so a class has one __construct. Use optional parameters with defaults, or add static factory methods such as public static function fromArray(array $data): static that call new static(...) with the right arguments.

Is the parent constructor called automatically in PHP?

Only when the child class has no constructor of its own. If the child defines __construct, the parent's constructor does not run unless you call parent::__construct(...) yourself, usually as the first line.

Can a PHP constructor return a value?

No. new always gives you the object, and whatever __construct returns is ignored. To report a problem, throw an exception from the constructor, for example throw new InvalidArgumentException('Age must be positive').

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED