1.2.1441. Wrong Access Style to Property¶
Use the right syntax when reaching for a property. Static properties use the \:\:
operator, and non-static properties use ->
.
Mistaking one of the other raise two different reactions from PHP : Access to undeclared `static <https://www.php.net/manual/en/language.oop5.static.php>`_ property
is a fatal error, while PHP Notice: Accessing `static <https://www.php.net/manual/en/language.oop5.static.php>`_ property aa\:\:$a as non `static <https://www.php.net/manual/en/language.oop5.static.php>`_
is a notice.
This analysis reports both static properties with a -> access, and non-static properties with a :: access.
<?php
class a {
static public $a = 1;
function foo() {
echo self::$a; // right
echo $this->a; // WRONG
}
}
class b {
public $b = 1;
function foo() {
echo $this->$b; // right
echo b::$b; // WRONG
}
}
?>
See also Static Keyword.
1.2.1441.2. Connex PHP features¶
1.2.1441.2.1. Suggestions¶
Match the property call with the definition
Make the property static
1.2.1441.2.2. Specs¶
Short name |
Classes/UndeclaredStaticProperty |
Rulesets |
|
Exakat since |
1.4.9 |
PHP Version |
All |
Severity |
Critical |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Examples |
|
Available in |