Constructors
A constructor initializes an object; promotion declares and assigns properties.
What it is
A constructor initializes an object; promotion declares and assigns properties.
When to use it
Require valid dependencies and state at creation.
Syntax
public function __construct(private PaymentGateway $gateway) {}Example
class Checkout
{
public function __construct(private PaymentGateway $gateway) {}
public function pay(int $amount): bool
{
return $this->gateway->charge($amount);
}
}Common mistakes
Avoid network calls in constructors; dependency injection does not require an interface for every class.