🐘 PHP Beginner

What is the __construct() method in PHP?

Answer

The __construct() method is PHP's constructor — it is automatically called when an object is created with new. It is used to initialize object properties and perform setup. PHP 8.0 introduced constructor property promotion, which dramatically reduces boilerplate: class User { public function __construct(private string $name, private int $age) {} } — declaring the parameter with an access modifier automatically creates and assigns the property. PHP only supports one constructor per class (no overloading), but you can use default parameter values to simulate multiple constructor signatures. The counterpart is __destruct(), called when the object is destroyed.