Skip to content

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
wants to merge 13 commits into from
38 changes: 1 addition & 37 deletions Zend/tests/030.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,4 @@ $test->bar();

?>
--EXPECTF--
object(Exception)#%d (7) {
["message":protected]=>
string(3) "foo"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(%d) "%s030.php"
["line":protected]=>
int(%d)
["trace":"Exception":private]=>
array(1) {
[0]=>
array(6) {
["file"]=>
string(%d) "%s030.php"
["line"]=>
int(%d)
["function"]=>
string(3) "bar"
["class"]=>
string(3) "foo"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
}
["previous":"Exception":private]=>
NULL
}
'test' => '0'
'test_2' => '1'
'test_3' => '2'
ok
Fatal error: Cannot re-assign $this in %s030.php on line 11
4 changes: 1 addition & 3 deletions Zend/tests/bug68370.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ $x = $c->test();
print_r($x);
unset($c, $x);
--EXPECTF--
Array
(
)
Fatal error: Cannot unset $this in %sbug68370.php on line 4
34 changes: 19 additions & 15 deletions Zend/tests/fr47160.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Calling method from array

class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
echo "Hello, $x\n";return $this;
}
}

Expand Down Expand Up @@ -37,8 +37,16 @@ class Magic3 {
}

$f = array('Hello','world');
var_dump($f('you'));
var_dump(call_user_func($f, 'you'));
try {
var_dump($f('you'));
} catch (Throwable $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
try {
var_dump(call_user_func($f, 'you'));
} catch (Throwable $e) {
echo "Exception: " . $e->getMessage() . "\n";
}

printf("-----\n");

Expand Down Expand Up @@ -101,35 +109,31 @@ var_dump(call_user_func($f, 'you'));
--EXPECTF--
Deprecated: Non-static method Hello::world() should not be called statically in %s on line %d
Hello, you

Notice: Undefined variable: this in %s on line %d
NULL
Exception: Using $this when not in object context

Deprecated: %son-static method Hello::world() should not be called statically in %s on line %d
Hello, you

Notice: Undefined variable: this in %s on line %d
NULL
Exception: Using $this when not in object context
-----
Hello, again
object(Hello)#1 (0) {
object(Hello)#%d (0) {
}
Hello, again
object(Hello)#1 (0) {
object(Hello)#%d (0) {
}
-----
Hello, there
object(Hello)#2 (0) {
object(Hello)#%d (0) {
}
Hello, there
object(Hello)#2 (0) {
object(Hello)#%d (0) {
}
-----
Hello, devs
object(Hello)#4 (0) {
object(Hello)#%d (0) {
}
Hello, devs
object(Hello)#4 (0) {
object(Hello)#%d (0) {
}
-----
Magic::__call called (foo)!
Expand Down
11 changes: 6 additions & 5 deletions Zend/tests/incompat_ctx_user.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class B {
function bar() { A::foo(); }
}
$b = new B;
$b->bar();

try {
$b->bar();
} catch (Throwable $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
?>
--EXPECTF--
Deprecated: Non-static method A::foo() should not be called statically in %s on line %d

Notice: Undefined variable: this in %s on line %d
string(1) "A"
Exception: Using $this when not in object context
11 changes: 6 additions & 5 deletions Zend/tests/indirect_call_array_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class foo {
}

$arr = array('foo', 'abc');
$arr();

try {
$arr();
} catch (Throwable $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
$foo = new foo;
$arr = array($foo, 'abc');
$arr();
Expand All @@ -28,9 +31,7 @@ $arr();
--EXPECTF--
From foo::__callStatic:
string(3) "abc"

Notice: Undefined variable: this in %s on line %d
NULL
Exception: Using $this when not in object context
From foo::__call:
string(3) "abc"
object(foo)#%d (0) {
Expand Down
12 changes: 12 additions & 0 deletions Zend/tests/this_as_global.phpt
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
11 changes: 11 additions & 0 deletions Zend/tests/this_as_parameter.phpt
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
12 changes: 12 additions & 0 deletions Zend/tests/this_as_static.phpt
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
18 changes: 18 additions & 0 deletions Zend/tests/this_in_catch.phpt
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
17 changes: 17 additions & 0 deletions Zend/tests/this_in_extract.phpt
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
11 changes: 11 additions & 0 deletions Zend/tests/this_in_foreach_001.phpt
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
Copy link
Contributor

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.

Copy link
Member Author

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.

11 changes: 11 additions & 0 deletions Zend/tests/this_in_foreach_002.phpt
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
11 changes: 11 additions & 0 deletions Zend/tests/this_in_foreach_003.phpt
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
11 changes: 11 additions & 0 deletions Zend/tests/this_in_foreach_004.phpt
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
41 changes: 41 additions & 0 deletions Zend/tests/this_in_isset.phpt
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)
19 changes: 19 additions & 0 deletions Zend/tests/this_in_mb_parse_str.phpt
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
17 changes: 17 additions & 0 deletions Zend/tests/this_in_parse_str.phpt
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
8 changes: 8 additions & 0 deletions Zend/tests/this_in_unset.phpt
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
17 changes: 17 additions & 0 deletions Zend/tests/this_reassign.phpt
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
Loading