← BACK
PHP / Syntax

Simplify Class Instantiation with Constructor Property Promotion

Punyapal Shah • • 1 min read
edit this tip →

Combine property declarations and constructor parameter assignments in PHP 8 for concise class definitions.

Declaring class properties, parameter signatures, and manual $this->prop = $prop assignments creates boilerplate code. Constructor property promotion combines all three steps in the parameter list.

namespace App\Services;

class PaymentProcessor
{
    // Property promotion combines declaration, type, and assignment
    public function __construct(
        public readonly StripeClient $client,
        private string $apiKey,
        protected int $timeout = 30,
    ) {}
}
  • Eliminates repetitive property definitions and assignment statements
  • Supports visibility modifiers (public, protected, private) and readonly flags
  • Fully compatible with docblocks, attributes, and default values

Related Tips

View all tips →

// Got a tip in mind? Contributions are always welcome