Content · Web Development · PHP

PHP Constructor Property Promotion

PHP Constructor Property Promotion: Constructor promotion declares and initializes properties directly in constructor parameters. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Constructor promotion declares and initializes properties directly in constructor parameters.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP Constructor Property Promotion Works


Constructor promotion declares and initializes properties directly in constructor parameters.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

class Post { public function __construct(public string $title, public int $views = 0) {} }
echo (new Post("Intro"))->title;

Output:

Intro

Constructor promotion declares and initializes properties directly in constructor parameters.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

class UserAccount { public function __construct(private string $token) {} public function tokenLength(): int { return strlen($this->token); } }
echo (new UserAccount("secret"))->tokenLength();

Output:

6

Promoted properties obey visibility and type rules, and promotion is not automatically validation.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

final class ProductItem { public function __construct(public string $sku, public float $price) {} }
echo (new ProductItem("SKU-5", 19.5))->sku;

Output:

SKU-5

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Promoted properties obey visibility and type rules, and promotion is not automatically validation.
  • Constructor promotion declares and initializes properties directly in constructor parameters.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP Constructor Property PromotionConstructor promotion declares and initializes properties directly in constructor parameters.
Alternative approachTraditional property declarations remain useful for fields not passed through a constructor.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Constructor Property PromotionPromoted properties obey visibility and type rules, and promotion is not automatically validation.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyTraditional property declarations remain useful for fields not passed through a constructor.

Key Takeaways


  • Constructor promotion declares and initializes properties directly in constructor parameters.
  • Promoted properties obey visibility and type rules, and promotion is not automatically validation.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP Constructor Property Promotion is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.