1.2.1431. 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.1431.1. Suggestions

  • Match the property call with the definition

  • Make the property static

1.2.1431.2. Specs

Short name

Classes/UndeclaredStaticProperty

Rulesets

All, Analyze, CE, CI-checks, Class Review

Exakat since

1.4.9

PHP Version

All

Severity

Critical

Time To Fix

Quick (30 mins)

Precision

Very high

Features

declaration

Examples

HuMo-Gen

Available in

Entreprise Edition, Community Edition, Exakat Cloud