Skip to content

Commit a66c60c

Browse files
committed
Throw Error when writing property of non-object
This removes object auto-vivification support. This also means that we can remove the corresponding special handling for typed properites: We no longer need to check that a property is convertible to stdClass if such a conversion might take place indirectly due to a nested property write. Additionally OBJ_W style operations now no longer modify the object operand, and as such we no longer need to treat op1 as a def in SSA form. The next step would be to actually compile the whole LHS of OBJ_W operations in R rather than W mode, but that causes issues with SimpleXML, whose object handlers depend on the current compilation structure. Part of https://wiki.php.net/rfc/engine_warnings.
1 parent e8d2097 commit a66c60c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+580
-1416
lines changed

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ PHP 8.0 UPGRADE NOTES
8686
function test(?int $arg = CONST_RESOLVING_TO_NULL) {}
8787
// Or
8888
function test(int $arg = null) {}
89+
. Attempting to write to a property of a non-object will now result in an
90+
Error exception. Previously this resulted in a warning and either converted
91+
the value into an object (if it was null, false or an empty string) or
92+
ignored the write altogether (in all other cases).
93+
RFC: Part of https://wiki.php.net/rfc/engine_warnings
8994

9095
- COM:
9196
. Removed the ability to import case-insensitive constants from type

Zend/tests/026.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ $test = new foo;
1313
$test->a()->a;
1414
print "ok\n";
1515

16-
$test->a()->a = 1;
16+
try {
17+
$test->a()->a = 1;
18+
} catch (Error $e) {
19+
echo $e->getMessage(), "\n";
20+
}
1721
print "ok\n";
1822

1923
?>
2024
--EXPECTF--
2125
Notice: Trying to get property 'a' of non-object in %s on line %d
2226
ok
23-
24-
Warning: Creating default object from empty value in %s on line %d
27+
Attempt to assign property 'a' of non-object
2528
ok

Zend/tests/033.phpt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ echo $arr[1][2][3][4][5];
99

1010
$arr[1][2][3][4][5]->foo;
1111

12-
$arr[1][2][3][4][5]->foo = 1;
12+
try {
13+
$arr[1][2][3][4][5]->foo = 1;
14+
} catch (Error $e) {
15+
echo $e->getMessage(), "\n";
16+
}
1317

1418
$arr[][] = 2;
1519

16-
$arr[][]->bar = 2;
20+
try {
21+
$arr[][]->bar = 2;
22+
} catch (Error $e) {
23+
echo $e->getMessage(), "\n";
24+
}
1725

1826
?>
1927
--EXPECTF--
@@ -54,7 +62,5 @@ Notice: Trying to access array offset on value of type null in %s on line %d
5462
Notice: Trying to access array offset on value of type null in %s on line %d
5563

5664
Notice: Trying to get property 'foo' of non-object in %s on line %d
57-
58-
Warning: Creating default object from empty value in %s on line %d
59-
60-
Warning: Creating default object from empty value in %s on line %d
65+
Attempt to assign property 'foo' of non-object
66+
Attempt to assign property 'bar' of non-object

Zend/tests/assign_dim_obj_null_return.phpt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ function test() {
1717
var_dump($array[new stdClass] += 123);
1818
var_dump($true[123] += 456);
1919

20-
var_dump($true->foo = 123);
21-
var_dump($true->foo += 123);
20+
try {
21+
var_dump($true->foo = 123);
22+
} catch (Error $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
try {
26+
var_dump($true->foo += 123);
27+
} catch (Error $e) {
28+
echo $e->getMessage(), "\n";
29+
}
2230
}
2331

2432
test();
@@ -48,9 +56,5 @@ NULL
4856

4957
Warning: Cannot use a scalar value as an array in %s on line %d
5058
NULL
51-
52-
Warning: Attempt to assign property 'foo' of non-object in %s on line %d
53-
NULL
54-
55-
Warning: Attempt to assign property 'foo' of non-object in %s on line %d
56-
NULL
59+
Attempt to assign property 'foo' of non-object
60+
Attempt to assign property 'foo' of non-object

Zend/tests/assign_ref_error_var_handling.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ function val() {
77
return 42;
88
}
99

10-
$str = "foo";
1110
$var = 24;
12-
var_dump($str->foo =& $var);
13-
var_dump($str);
14-
var_dump($str->foo =& val());
15-
var_dump($str);
11+
$arr = [PHP_INT_MAX => "foo"];
12+
var_dump($arr[] =& $var);
13+
var_dump(count($arr));
14+
var_dump($arr[] =& val());
15+
var_dump(count($arr));
1616

1717
?>
1818
--EXPECTF--
19-
Warning: Attempt to modify property 'foo' of non-object in %s on line %d
19+
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
2020
NULL
21-
string(3) "foo"
21+
int(1)
2222

23-
Warning: Attempt to modify property 'foo' of non-object in %s on line %d
23+
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
2424
NULL
25-
string(3) "foo"
25+
int(1)

Zend/tests/bug36303.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
Bug #36303 (foreach on error_zval produces segfault)
33
--FILE--
44
<?php
5-
$x="test";
6-
foreach($x->a->b as &$v) {
5+
$x=[PHP_INT_MAX=>"test"];
6+
foreach ($x[] as &$v) {
77
}
88
echo "ok\n";
99
?>
1010
--EXPECTF--
11-
Warning: Attempt to modify property 'a' of non-object in %sbug36303.php on line 3
11+
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
1212

13-
Warning: Invalid argument supplied for foreach() in %sbug36303.php on line 3
13+
Warning: Invalid argument supplied for foreach() in %s on line %d
1414
ok

Zend/tests/bug41075.phpt

Lines changed: 0 additions & 31 deletions
This file was deleted.

Zend/tests/bug44660.phpt

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,62 @@ $a = true;
88
echo "--> read access: ";
99
echo $a->p;
1010

11-
echo "\n--> direct assignment: ";
12-
$a->p = $s;
11+
echo "\n--> direct assignment:\n";
12+
try {
13+
$a->p = $s;
14+
} catch (Error $e) {
15+
echo $e->getMessage(), "\n";
16+
}
1317

14-
echo "\n--> increment: ";
15-
$a->p++;
18+
echo "\n--> increment:\n";
19+
try {
20+
$a->p++;
21+
} catch (Error $e) {
22+
echo $e->getMessage(), "\n";
23+
}
1624

17-
echo "\n--> reference assignment:";
18-
$a->p =& $s;
25+
echo "\n--> reference assignment:\n";
26+
try {
27+
$a->p =& $s;
28+
} catch (Error $e) {
29+
echo $e->getMessage(), "\n";
30+
}
1931

20-
echo "\n--> reference assignment:";
21-
$s =& $a->p;
32+
echo "\n--> reference assignment:\n";
33+
try {
34+
$s =& $a->p;
35+
} catch (Error $e) {
36+
echo $e->getMessage(), "\n";
37+
}
2238

23-
echo "\n--> indexed assignment:";
24-
$a->p[0] = $s;
39+
echo "\n--> indexed assignment:\n";
40+
try {
41+
$a->p[0] = $s;
42+
} catch (Error $e) {
43+
echo $e->getMessage(), "\n";
44+
}
2545

2646
echo "\n--> Confirm assignments have had no impact:\n";
2747
var_dump($a);
2848
?>
2949
--EXPECTF--
3050
--> read access:
31-
Notice: Trying to get property 'p' of non-object in %sbug44660.php on line 6
51+
Notice: Trying to get property 'p' of non-object in %s on line %d
3252

33-
--> direct assignment:
34-
Warning: Attempt to assign property 'p' of non-object in %sbug44660.php on line 9
53+
--> direct assignment:
54+
Attempt to assign property 'p' of non-object
3555

36-
--> increment:
37-
Warning: Attempt to increment/decrement property 'p' of non-object in %sbug44660.php on line 12
56+
--> increment:
57+
Attempt to increment/decrement property 'p' of non-object
3858

3959
--> reference assignment:
40-
Warning: Attempt to modify property 'p' of non-object in %sbug44660.php on line 15
60+
Attempt to modify property 'p' of non-object
4161

4262
--> reference assignment:
43-
Warning: Attempt to modify property 'p' of non-object in %sbug44660.php on line 18
63+
Attempt to modify property 'p' of non-object
4464

4565
--> indexed assignment:
46-
Warning: Attempt to modify property 'p' of non-object in %sbug44660.php on line 21
66+
Attempt to modify property 'p' of non-object
4767

4868
--> Confirm assignments have had no impact:
4969
bool(true)

Zend/tests/bug48004.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)