1.2.298. Could Use Promoted Properties

Promoted properties are a syntax notation where the properties are declared as arguments of the constructor.

They reduce PHP code at __construct() time. This feature is available in PHP 8.0.

<?php

class x {
    function __construct($a, $b) {
        // $a argument may be promoted to property $c
        $this->c = $a;

        // $b argument cannot be upgraded to property, as it is updated.
        // Move the addition to the new call, or keep the syntax below
        $this->d = $b + 2;
    }
}

?>

See also PHP 8: Constructor property promotion and PHP RFC: Constructor Property Promotion.

1.2.298.1. Connex PHP features

1.2.298.1.1. Suggestions

  • Update the constructor syntax, and remove the property specification.

1.2.298.1.2. Specs

Short name

Php/CouldUsePromotedProperties

Rulesets

All, Suggestions

Exakat since

2.1.9

PHP Version

With PHP 8.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud