1.2.1146. Static Methods Can’t Contain $this

Static methods are also called class methods : they may be called even if the class has no instantiated object. Thus, the local variable $this won’t exist, PHP will set it to NULL as usual.

Either this is not a static method, which is fixed by removing the static keyword, or replace all $this mention by static properties Class\:\:$property.

<?php

class foo {
    // Static method may access other static methods, or property, or none.
    static function staticBar() {
        // This is not possible in a static method
        return self::otherStaticBar() . static::$staticProperty;
    }

    static function bar() {
        // This is not possible in a static method
        return $this->property;
    }
}

?>

See also Static Keyword <https://www.php.net/manual/en/language.oop5.static.php>``Static Keyword.

1.2.1146.1. Suggestions

  • Remove any $this usage

  • Turn any $this usage into a static call : $this->foo() => self::foo()

1.2.1146.2. Specs

Short name

Classes/StaticContainsThis

Rulesets

All, Analyze, CE, CI-checks

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

$this, static

ClearPHP

no-static-this

Examples

xataface, SugarCrm

Available in

Entreprise Edition, Community Edition, Exakat Cloud