1.2.327. Declare Static Once

Global and static variables should be declared only once in a method. It is also recommended to configure it at the beginning of the method. This could be refined by defining the variable at the last common moment, though it lacks readability.

Defining static or global methods late is a micro-optimisation.

<?php

function foo() {
    if (rand(0, 1)) {
        static $x;

        ++$x;
    } else {
        static $x;

        --$x;
    }
}

?>

1.2.327.1. Suggestions

  • Remove duplicate static and global calls

  • Move the static and global calls to the beginning of the method

  • Refactor the static and global variable to properties

1.2.327.2. Specs

Short name

Structures/DeclareStaticOnce

Rulesets

All, Suggestions

Exakat since

2.2.1

PHP Version

With PHP 8.3 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

static

Available in

Entreprise Edition, Exakat Cloud