What is Constructor Property Promotion in PHP 8, and how does it change class definitions?
Last updated
Constructor Property Promotion (PHP 8) lets you declare and assign a property directly in the constructor signature by adding a visibility modifier (
public,protected,private, orreadonly) to a parameter.It removes boilerplate: no separate property declarations and no
$this->x = $x;lines, which is ideal for data/value objects.Limits: it only works in the constructor, can't promote
callableparams, and can't duplicate a property declared elsewhere. A constructor body for validation is still allowed.