Constructor automatically initializes object properties when an instance is created.
Defined by __construct()
Syntax-
class MyClass {
public function __construct($param1, $param2) { /* constructor code */ }
}
$obj = new MyClass($value1, $value2);
Example-
class Car {
public $brand;
public $color;
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
}
$car = new Car("Toyota", "Blue");
Constructor sets brand to "Toyota" and color to "Blue"