1.2.5. $this Belongs To Classes Or Traits

The pseudo-variable $this must be used inside a class or trait, or bound closures.

$this variable represents the current object, inside a class or trait scope

It is a pseudo-variable, and should be used within class’s or trait’s methods and not outside. It should also not be used in static methods.

PHP 7.1 is stricter and check for $this at several situations.

<?php

// as an argument
function foo($this) {
    // Using global
    global $this;
    // Using static (not a property)
    static $this;

    // Can't unset it
    unset($this);

    try {
        // inside a foreach
        foreach($a as $this) {  }
        foreach($a as $this => $b) {  }
        foreach($a as $b => $this) {  }
    } catch (Exception $this) {
        // inside a catch
    }

    // with Variable Variable
    $a = this;
    $$a = 42;
}

class foo {
    function bar() {
        // Using references
        $a =& $this;
        $a = 42;

        // Using extract(), parse_str() or similar functions
        extract([this => 42]);  // throw new Error(Cannot re-assign $this)
        var_dump($this);
    }

    static function __call($name, $args) {
        // Using __call
        var_dump($this); // prints object(C)#1 (0) {}, php-7.0 printed NULL
        $this->test();   // prints ops
    }

}
?>

See also class.

1.2.5.1. Suggestions

  • Do not use $this as a variable name, except for the current object, in a class, trait or closure.

1.2.5.2. Specs

Short name

Classes/ThisIsForClasses

Rulesets

All, Analyze, Changed Behavior, LintButWontExec

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

$this, self, parent, static

Examples

OpenEMR

Note

This issue may lint but will not run

Available in

Entreprise Edition, Exakat Cloud