$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 in /codes/thisMustBeInObject.php on line 3
Notice: Undefined variable: this in /codes/thisMustBeInObject.php on line 3
NULL
After¶
PHP Fatal error: Uncaught Error: Using $this when not in object context in /codes/thisMustBeInObject.php:3
Stack trace:
#0 {main}
thrown in /codes/thisMustBeInObject.php on line 3
Fatal error: Uncaught Error: Using $this when not in object context in /codes/thisMustBeInObject.php:3
Stack trace:
#0 {main}
thrown in /codes/thisMustBeInObject.php on line 3
PHP version change¶
This behavior changed in 7.1