PHP Constructor

A constructor is a special method that PHP runs automatically the moment an object is created, letting you set up its starting state.

What a constructor does

When you create an object with new, you often want it to start in a valid, ready-to-use state. A constructor is a method named __construct that PHP calls for you at that exact moment. Any arguments you pass inside the parentheses of new are handed to the constructor, so you can require the data an object needs in order to exist.

A basic constructor

<?php
class Product
{
    public string $name;
    public float $price;

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

$book = new Product("PHP Handbook", 29.99);
echo $book->name . " costs " . $book->price; // PHP Handbook costs 29.99
?>

Notice that the constructor removes the need to set each property on a separate line after creating the object. Because the name and price are required arguments, it is now impossible to create a Product without them, which makes the object safer to use elsewhere in your code.

Constructor property promotion

Since PHP 8.0 you can declare and assign a property directly in the constructor's parameter list. This is called constructor property promotion, and it removes the repetitive pattern of declaring a property, then assigning it from a matching argument. Add an access modifier such as public in front of the parameter, and PHP creates the property and fills it automatically.

Promoted constructor properties (PHP 8+)

<?php
class Product
{
    public function __construct(
        public string $name,
        public float $price = 0.0
    ) {
    }

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

$pen = new Product("Blue Pen", 1.50);
echo $pen->label(); // Blue Pen: $1.5
?>
  • The constructor method must be named exactly __construct with two leading underscores.
  • It runs once per object, automatically, right after the object is created.
  • Parameters can have default values, making some arguments optional.
  • You can only have one constructor per class; PHP does not support several with different signatures.

Validating input in the constructor

A constructor is a good place to reject bad data so an object can never exist in an invalid state. If a required value is out of range, throwing an exception stops the object from being created and signals the problem early.

Guarding against invalid values

<?php
class Temperature
{
    public function __construct(public float $celsius)
    {
        if ($celsius < -273.15) {
            throw new InvalidArgumentException("Below absolute zero.");
        }
    }
}

$t = new Temperature(21.5);
echo $t->celsius; // 21.5
?>
Note: If a child class defines its own constructor, PHP does not automatically call the parent's constructor. When you need the parent's setup to run too, call parent::__construct() yourself from inside the child constructor.