Promoted Properties¶
Promoted properties are a class’s arguments, which are declared as properties and automatically assigned their value at instantiation.
<?php
class X {
function __construct($property, public int $promotedProperty) {
$this->property = $property; // manual initialization
// not initilization of $this->promotedProperty, as it is automatic
echo $promotedProperty; // the variable version is available for further processing
echo $this->promotedProperty; // the property is available immediately
}
}
?>
See also Class constructor property promotion.
Related : Properties, Class, Constructor, Property Type Declaration, Var
Added in PHP 8.0