PHPCompatibility Code

PHPCompatibility.Variables.ForbiddenThisUseContexts.OutsideObjectContext

Recommendation

At the time of writing, PHPCompatibility incorrectly flags some code with this error.

If you're using $this directly, such as in the following example, then you can safely replace it with null:

function foo() {
   return $this;
}
var_dump(foo());

In the above case, PHP 7.1 will throw an error. Older versions will treat $this as an undefined variable and output null:

If you're accessing a method or property, such as in the following example, then this is a pre-existing bug that has nothing to do with PHP 7.1:

function foo() {
   return $this->property;
}
var_dump(foo());

In the above case, you would get a fatal error in PHP versions 5.0 and higher. You either have fatal errors that you missed, or that code is unreachable. The solution will depend on the intent of the original code, so it will require manual investigation for each occurrence.