$this Must Be The Local Object¶
$this used to be a variable like any other, except for being filled with the local object in a method.
Since PHP 7.1, it is now only used for this purpose. This means that $this cannot be used outside a class, an enumeration or a trait, and for any other purpose.
PHP code¶
<?php
var_dump($this);
?>
Before¶
PHP Notice: Undefined variable: this
Notice: Undefined variable: this
NULL
After¶
PHP Fatal error: Uncaught Error: Using $this when not in object context
Fatal error: Uncaught Error: Using $this when not in object context
PHP version change¶
This behavior changed in 7.1