-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Fix inconsistent $this behavior #1918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
41f1531
Fixed inconsistent $this behavior
dstogov e32cc52
Throw exception on attempt to re-assign $this through extract() and p…
dstogov d03a9a1
Merge branch 'master' into this_var
dstogov 605a237
Merge branch 'master' into this_var
dstogov fa08813
Changed "Notice: Undefined variable: this" into "Exception: Using $th…
dstogov b8579f8
Merge branch 'master' into this_var
dstogov 73f8d14
Restored PHP-7 behavior of isset($this->foo).
dstogov 59a9a6c
Added type inference rule for FETCH_THIS opcode
dstogov cf749c4
Protection from $this reassign through mb_parse_str()
dstogov ae7663c
Merge branch 'master' into this_var
dstogov 2f1d7c8
Removed unused variable
dstogov 09e676f
Merge branch 'master' into this_var
dstogov bdd3b68
Fixed GOTO VM
dstogov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
$this as global variable | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
global $this; | ||
var_dump($this); | ||
} | ||
foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use $this as global variable in %sthis_as_global.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
$this as parameter | ||
--FILE-- | ||
<?php | ||
function foo($this) { | ||
var_dump($this); | ||
} | ||
foo(5); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use $this as parameter in %sthis_as_parameter.php on line 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
$this as static variable | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
static $this; | ||
var_dump($this); | ||
} | ||
foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use $this as static variable in %sthis_as_static.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
$this in catch | ||
--FILE-- | ||
<?php | ||
class C { | ||
function foo() { | ||
try { | ||
throw new Exception(); | ||
} catch (Exception $this) { | ||
} | ||
var_dump($this); | ||
} | ||
} | ||
$obj = new C; | ||
$obj->foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot re-assign $this in %sthis_in_catch.php on line 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
$this re-assign in extract() | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
extract(["this"=>42]); | ||
var_dump($this); | ||
} | ||
foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Cannot re-assign $this in %sthis_in_extract.php:3 | ||
Stack trace: | ||
#0 %sthis_in_extract.php(3): extract(Array) | ||
#1 %sthis_in_extract.php(6): foo() | ||
#2 {main} | ||
thrown in %sthis_in_extract.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
$this in foreach | ||
--FILE-- | ||
<?php | ||
$a = [1]; | ||
foreach ($a as $this) { | ||
var_dump($this); | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot re-assign $this in %sthis_in_foreach_001.php on line 3 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
$this in foreach | ||
--FILE-- | ||
<?php | ||
$a = [1]; | ||
foreach ($a as $this => $dummy) { | ||
var_dump($this); | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot re-assign $this in %sthis_in_foreach_002.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
$this in foreach | ||
--FILE-- | ||
<?php | ||
$a = [1]; | ||
foreach ($a as &$this) { | ||
var_dump($this); | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot re-assign $this in %sthis_in_foreach_003.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
$this in foreach | ||
--FILE-- | ||
<?php | ||
$a = [[1]]; | ||
foreach ($a as list($this)) { | ||
var_dump($this); | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot re-assign $this in %sthis_in_foreach_004.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
$this in isset | ||
--FILE-- | ||
<?php | ||
var_dump(isset($this)); | ||
try { | ||
var_dump(isset($this->foo)); | ||
} catch (Throwable $e) { | ||
echo "exception\n"; | ||
} | ||
try { | ||
var_dump(isset($this->foo->bar)); | ||
} catch (Throwable $e) { | ||
echo "exception\n"; | ||
} | ||
try { | ||
var_dump(isset($this[0])); | ||
} catch (Throwable $e) { | ||
echo "exception\n"; | ||
} | ||
|
||
class A extends ArrayObject { | ||
public $foo = 5; | ||
function foo() { | ||
$this[0] = 5; | ||
var_dump(isset($this)); | ||
var_dump(isset($this->foo)); | ||
var_dump(isset($this[0])); | ||
} | ||
} | ||
$a = new A(); | ||
$a->foo(); | ||
?> | ||
--EXPECT-- | ||
bool(false) | ||
exception | ||
exception | ||
exception | ||
bool(true) | ||
bool(true) | ||
bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
$this re-assign in mb_parse_str() | ||
--SKIPIF-- | ||
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?> | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
mb_parse_str("this=42"); | ||
var_dump($this); | ||
} | ||
foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Cannot re-assign $this in %sthis_in_mb_parse_str.php:3 | ||
Stack trace: | ||
#0 %sthis_in_mb_parse_str.php(3): mb_parse_str('this=42') | ||
#1 %sthis_in_mb_parse_str.php(6): foo() | ||
#2 {main} | ||
thrown in %sthis_in_mb_parse_str.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
$this re-assign in parse_str() | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
parse_str("this=42"); | ||
var_dump($this); | ||
} | ||
foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Cannot re-assign $this in %sthis_in_parse_str.php:3 | ||
Stack trace: | ||
#0 %sthis_in_parse_str.php(3): parse_str('this=42') | ||
#1 %sthis_in_parse_str.php(6): foo() | ||
#2 {main} | ||
thrown in %sthis_in_parse_str.php on line 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
$this in unset | ||
--FILE-- | ||
<?php | ||
unset($this); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot unset $this in %sthis_in_unset.php on line 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
$this re-assign | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
$a = "this"; | ||
$$a = 0; | ||
var_dump($$a); | ||
} | ||
foo(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Cannot re-assign $this in %sthis_reassign.php:4 | ||
Stack trace: | ||
#0 %sthis_reassign.php(7): foo() | ||
#1 {main} | ||
thrown in %sthis_reassign.php on line 4 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message is a bit strange. Since there is no
$this
in current scope,re-assign
in the message doesn't make sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we used the same message for similar problems in 7.0 and below.