| aharvey | Yes | 2016-06-06 22:50 |
| ajf | Yes | 2016-06-07 04:34 |
| bishop | Yes | 2016-06-06 14:13 |
| bwoebi | Yes | 2016-06-06 09:59 |
| colinodell | Yes | 2016-06-06 14:47 |
| daverandom | Yes | 2016-06-06 11:29 |
| davey | Yes | 2016-06-06 23:41 |
| dm | Yes | 2016-06-07 20:31 |
| dmitry | Yes | 2016-06-06 07:05 |
| dragoonis | Yes | 2016-06-07 07:26 |
| fa | Yes | 2016-06-06 09:47 |
| galvao | Yes | 2016-06-08 00:10 |
| guilhermeblanco | Yes | 2016-06-10 12:59 |
| hywan | Yes | 2016-06-06 08:44 |
| jpauli | Yes | 2016-06-07 09:10 |
| kguest | Yes | 2016-06-09 12:37 |
| krakjoe | Yes | 2016-06-07 05:35 |
| laruence | Yes | 2016-06-07 05:47 |
| lcobucci | Yes | 2016-06-10 09:56 |
| leigh | Yes | 2016-06-13 12:38 |
| levim | Yes | 2016-06-07 16:39 |
| lstrojny | Yes | 2016-06-07 07:15 |
| marcio | Yes | 2016-06-06 22:22 |
| mariano | Yes | 2016-06-06 14:24 |
| mattwil | Yes | 2016-06-06 20:48 |
| mbeccati | Yes | 2016-06-06 07:41 |
| mrook | Yes | 2016-06-09 21:08 |
| nikic | Yes | 2016-06-06 10:14 |
| ocramius | Yes | 2016-06-06 07:33 |
| peehaa | Yes | 2016-06-11 21:24 |
| philstu | Yes | 2016-06-09 16:50 |
| pierrick | Yes | 2016-06-07 03:05 |
| pollita | Yes | 2016-06-07 00:16 |
| rasmus | Yes | 2016-06-14 22:35 |
| sammyk | Yes | 2016-06-07 16:32 |
| sebastian | Yes | 2016-06-08 06:55 |
| sobak | Yes | 2016-06-07 06:38 |
| thekid | Yes | 2016-06-06 20:23 |
| tpunt | Yes | 2016-06-06 09:15 |
| trowski | Yes | 2016-06-06 15:01 |
| yohgaki | Yes | 2016-06-06 09:44 |
| zeev | Yes | 2016-06-07 23:22 |
| zimt | Yes | 2016-06-07 08:41 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC
Historically PHP implementation accessed special $this variable using two different methods. In some cases this might lead to significant inconsistency, when $this accessed through different methods might have different values.
class C {
function foo() {
var_dump($this);
}
function bar() {
$a="this";
$$a=42;
var_dump($this); // prints int(42)
$this->foo(); // prints object(C)#1 (0) {}
}
}
$x = new C;
$x->bar();
This RFC proposes disabling modification of $this variable using “magic code” and make $this always behave in the same way.
The following code worked in PHP 7, but will emit compilation error in PHP 7.1
function foo($this) { // Fatal error: Cannot use $this as parameter
}
The following code leaded to “Cannot re-assign $this” compilation error. In PHP 7.1 it will produce more suitable error message - “Cannot use $this as static variable”.
static $this; // Fatal error: Cannot use $this as static variable
The following code worked in PHP 7, but will emit compilation error in PHP 7.1
global $this; // Fatal error: Cannot use $this as global variable
The following code worked in PHP 7, but will emit compilation error in PHP 7.1
try {
...
} catch (Exception $this) { // Fatal error: Cannot re-assign $this
}
The following code worked in PHP 7, but will emit compilation error in PHP 7.1
foreach ($a as $this) { // Fatal error: Cannot re-assign $this
}
It's not allowed to re-assign $this, so why it should be allowed to unset() it. The following code worked in PHP 7, but will emit compilation error in PHP 7.1
unset($this); // Fatal error: Cannot unset $this
An attempt to re-assign $this through $$ assignment will lead to throwing of Error exception.
$a = "this";
$$a = 42; // throw new Error("Cannot re-assign $this")
It's still possible to read $this value through $$.
Indirect re-assign $this through reference won't make effect in PHP 7.1
class C {
function foo(){
$a =& $this;
$a = 42;
var_dump($this); // prints object(C)#1 (0) {}, php-7.0 printed int(42)
}
}
$x = new C;
$x->foo();
Few internal PHP functions may re-assign local variable. In PHP 7.1 they cannot change value of $this variable and throw Error exception.
class C {
function foo(){
extract(["this"=>42]); // throw new Error("Cannot re-assign $this")
var_dump($this);
}
}
$x = new C;
$x->foo();
In PHP 7.0 and below get_defined_vars() might show or not show value of $this depending on some condition. (e.g. it was shown if we used $this variable itself, but not if it was used in a $this property reference or method call). In PHP 7.1 we won't show the value of $this in all cases.
In PHP 7.0 and below $this in static magic method __call() had value NULL. However it was possible to access properties and call object methods.
class C {
static function __call($name, $args) {
var_dump($this); // prints object(C)#1 (0) {}, php-7.0 printed NULL
$this->test(); // prints "ops"
}
function test() {
echo "ops\n";
}
}
$x = new C;
$x->foo();
Attempt to use $this in plain function or method now will lead to exception “Using $this when not in object context”. This unifies behavior with method call and property access. Previously PHP emitted “undefined variable” notice and continued execution assuming $this is NULL. It's still possible to use isset($this) and empty($this) to check object context.
function foo() {
var_dump($this); // throws "Using $this when not in object context"
// php-7.0 emitted "Undefined variable: this" and printed NULL
}
foo();
All the BC breaks are intentional and they are described in the proposal section.
PHP 7.1
none
none
The proposed implementation is compatible with opcache
none
The vote is a straight Yes/No vote, that requires a 2/3 majority. The voting began on Jun 6 and will close on Jun 16.
Vote: Fix inconsistent behavior of $this variable? — Yes: 43 · No: 0 (closed)
After the project is implemented, this section should contain