essentials:constructor_property_promotion

10. Constructor Property Promotion — Letting Objects Declare Themselves

Constructor Property Promotion (CPP) is one of the quiet revolutions of PHP 8.
It removes boilerplate and lets objects declare their shape directly in the constructor signature.

It reflects a broader shift in modern PHP:

objects should reveal their structure
without ceremony
.


1. What Constructor Property Promotion Is

CPP lets us define and initialize properties directly in the constructor parameters:

class User {
    public function __construct(
        public string $id,
        public string $email,
    ) {}
}

No separate property declarations.
No assignments inside the constructor.
No duplication.

Just the shape of the object, expressed cleanly.


2. Why Modern PHP Uses It

Constructor Property Promotion reduces:

  • boilerplate
  • duplication
  • noise around object shape
  • the distance between intention and expression

It makes objects easier to read, easier to reason about,
and easier for AI to generate correctly.


3. The Mental Model

CPP is structural shorthand.

It doesn’t change what the object is.
It simply removes the ceremony around declaring it.

The key idea:

  • Use CPP when the constructor’s purpose is to
    define the object’s shape.

If the constructor is doing real work — validating input, enforcing invariants, preparing state, or coordinating behavior — CPP may not be the right fit.
Promotion is for expressing structure, not for hiding logic.


4. A Simple Example

Without CPP:

class Point {
    public float $x;
    public float $y;
 
    public function __construct(float $x, float $y)
    {
        $this->x = $x;
        $this->y = $y;
    }
}

With CPP:

class Point {
    public function __construct(
        public float $x,
        public float $y,
    ) {}
}

Same meaning.
Less noise.
More clarity.


5. When to Use Constructor Property Promotion

Use CPP when:

  • the constructor only assigns properties
  • the object is a DTO or value object
  • the properties define the object’s identity
  • we want to reduce duplication
  • we want the shape of the object to be visible at a glance

CPP shines in DTOs, request models, value objects, and small domain primitives.


6. When Not to Use Constructor Property Promotion

Avoid CPP when:

  • the constructor performs work
  • validation or invariants belong in the constructor
  • the object has complex initialization logic
  • the properties shouldn’t be public
  • the object models behavior, not structure

CPP is for shape, not behavior.


When AI Gets This Wrong

AI often uses CPP everywhere, even when the constructor contains logic or invariants. Sometimes it mixes CPP with manual property declarations, or promotes properties that should remain private.

Our mental model helps us see when CPP expresses the object’s shape — and when AI is using it simply to reduce typing.


Summary

Constructor Property Promotion lets objects declare their structure without ceremony.
It reduces duplication, clarifies intent, and makes the shape of the object visible.

Once we internalize this, we can immediately see when AI‑generated code:

  • overuses CPP
  • mixes CPP with manual assignments
  • promotes properties that shouldn’t be public
  • or misses opportunities to simplify boilerplate

CPP is a small feature
— but it teaches us how modern PHP wants to express structure with clarity and intention.


essentials/constructor_property_promotion.txt · Last modified: by editor