1.2.328. Declare Global Early¶
Static and global keywords should be used as early as possible in a method.
Performance wise, it is better to call global
or static
only before using the variable.
Human-wise, it is recommended to put global
or static
at the beginning of the method, for better readability.
<?php
function foo() {
// $a is not global yet. It is a local variable
$a = 1;
// Same for static variables
$s = 5;
// Now $a is global
global $a;
$a = 3;
// Now $s is static
static $s;
$s = 55;
}
?>
See also Using static variables and The global keyword.
1.2.328.1. Connex PHP features¶
1.2.328.1.1. Suggestions¶
Use static and global at the beginning of the method
Move static and global to the first usage of the variable
Remove any access to the variable before static and global
1.2.328.1.2. Specs¶
Short name |
Structures/VariableMayBeNonGlobal |
Rulesets |
|
Exakat since |
1.5.3 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
Very high |
Available in |