Skip to content

Commit c42b7dd

Browse files
committed
Throw notice on array access on illegal type
No notice is thrown for list() accesses, because we did not come to an agreement regarding patterns like while ([$key, $value] = yield $it->next()) { ... } where silent null access may be desirable. No effort is made to suppress multiple notices in access chains likes $x[0][0][0], because the technical complexity this causes does not seem worthwhile. RFC: https://wiki.php.net/rfc/notice-for-non-valid-array-container
1 parent a634220 commit c42b7dd

19 files changed

+131
-14
lines changed

UPGRADING

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ PHP 7.4 UPGRADE NOTES
2222
========================================
2323

2424
- Core:
25+
. Trying to use values of type null, bool, int, float or resource as an
26+
array (such as $null["key"]) will now generate a notice. This does not
27+
affect array accesses performed by list().
2528
. get_declared_classes() no longer returns anonymous classes that haven't
2629
been instantiated yet.
2730
. "fn" is now a reserved keyword. In particular it can no longer be used as a
@@ -30,7 +33,6 @@ PHP 7.4 UPGRADE NOTES
3033
. Passing the result of a (non-reference) list() assignment by reference is
3134
consistently disallowed now. Previously this worked if the right hand side
3235
was a simple (CV) variable and did not occur as part of the list().
33-
. Added --ini-path and --ini-dir options to php-config.
3436

3537
- BCMath:
3638
. BCMath functions will now warn if a non well-formed number is passed, such

UPGRADING.INTERNALS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ PHP 7.4 INTERNALS UPGRADE NOTES
249249
PHP_LDAP_LFLAGS, PHP_LDAP_INCLUDE, PHP_LDAP_LIBS.
250250

251251
b. Unix build system changes
252+
- Added --ini-path and --ini-dir options to php-config.
252253
- configure --help now also outputs --program-suffix and --program-prefix
253254
information by using the Autoconf AC_ARG_PROGRAM macro.
254255
- Obsolescent macros AC_FUNC_VPRINTF and AC_FUNC_UTIME_NULL have been

Zend/tests/024.phpt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ var_dump($a->$b->{$c[1]});
1616
?>
1717
--EXPECTF--
1818
Notice: Undefined variable: a in %s on line %d
19+
20+
Notice: Trying to access array offset on value of type null in %s on line %d
1921
NULL
2022

21-
Notice: Undefined variable: %s in %s on line %d
23+
Notice: Undefined variable: a in %s on line %d
24+
25+
Notice: Undefined variable: c in %s on line %d
2226

23-
Notice: Undefined variable: %s in %s on line %d
27+
Notice: Trying to access array offset on value of type null in %s on line %d
2428
NULL
2529

2630
Notice: Undefined variable: a in %s on line %d
2731
int(1)
2832

29-
Notice: Undefined variable: %s in %s on line %d
33+
Notice: Undefined variable: a in %s on line %d
3034

31-
Notice: Undefined variable: %s in %s on line %d
35+
Notice: Undefined variable: b in %s on line %d
3236
int(0)
3337

3438
Notice: Undefined variable: a in %s on line %d
@@ -45,6 +49,8 @@ NULL
4549

4650
Notice: Undefined variable: c in %s on line %d
4751

52+
Notice: Trying to access array offset on value of type null in %s on line %d
53+
4854
Notice: Trying to get property '1' of non-object in %s on line %d
4955

5056
Notice: Trying to get property '' of non-object in %s on line %d

Zend/tests/033.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,40 @@ $arr[][]->bar = 2;
1919
--EXPECTF--
2020
Notice: Undefined variable: arr in %s on line %d
2121

22+
Notice: Trying to access array offset on value of type null in %s on line %d
23+
24+
Notice: Trying to access array offset on value of type null in %s on line %d
25+
26+
Notice: Trying to access array offset on value of type null in %s on line %d
27+
28+
Notice: Trying to access array offset on value of type null in %s on line %d
29+
30+
Notice: Trying to access array offset on value of type null in %s on line %d
31+
2232
Notice: Undefined variable: arr in %s on line %d
2333

34+
Notice: Trying to access array offset on value of type null in %s on line %d
35+
36+
Notice: Trying to access array offset on value of type null in %s on line %d
37+
38+
Notice: Trying to access array offset on value of type null in %s on line %d
39+
40+
Notice: Trying to access array offset on value of type null in %s on line %d
41+
42+
Notice: Trying to access array offset on value of type null in %s on line %d
43+
2444
Notice: Undefined variable: arr in %s on line %d
2545

46+
Notice: Trying to access array offset on value of type null in %s on line %d
47+
48+
Notice: Trying to access array offset on value of type null in %s on line %d
49+
50+
Notice: Trying to access array offset on value of type null in %s on line %d
51+
52+
Notice: Trying to access array offset on value of type null in %s on line %d
53+
54+
Notice: Trying to access array offset on value of type null in %s on line %d
55+
2656
Notice: Trying to get property 'foo' of non-object in %s on line %d
2757

2858
Warning: Creating default object from empty value in %s on line %d

Zend/tests/assign_to_var_003.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var_dump($var1);
1212

1313
echo "Done\n";
1414
?>
15-
--EXPECT--
15+
--EXPECTF--
16+
Notice: Trying to access array offset on value of type float in %s on line %d
1617
NULL
1718
NULL
1819
Done

Zend/tests/call_user_func_007.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var_dump($a);
1313
--EXPECTF--
1414
Notice: Undefined offset: 0 in %s on line %d
1515

16+
Notice: Trying to access array offset on value of type null in %s on line %d
17+
1618
Warning: Parameter 1 to foo() expected to be a reference, value given in %s on line %d
1719
array(0) {
1820
}

Zend/tests/dereference_002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ array(2) {
6969
int(5)
7070
}
7171
int(1)
72+
73+
Notice: Trying to access array offset on value of type int in %s on line %d
7274
NULL
7375

7476
Notice: Undefined offset: 4 in %s on line %d

Zend/tests/dereference_010.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ var_dump(b()[1]);
2121

2222
?>
2323
--EXPECTF--
24+
Notice: Trying to access array offset on value of type int in %s on line %d
2425
NULL
26+
27+
Notice: Trying to access array offset on value of type int in %s on line %d
2528
NULL
2629

2730
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d

Zend/tests/dereference_014.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ var_dump($h);
2727

2828
?>
2929
--EXPECTF--
30+
Notice: Trying to access array offset on value of type null in %s on line %d
31+
3032
Notice: Trying to get property 'a' of non-object in %s on line %d
3133
NULL
3234

35+
Notice: Trying to access array offset on value of type null in %s on line %d
36+
3337
Notice: Trying to get property 'b' of non-object in %s on line %d
3438
NULL

Zend/tests/isset_003.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Notice: Undefined variable: c in %s on line %d
3333

3434
Notice: Undefined variable: d in %s on line %d
3535

36+
Notice: Trying to access array offset on value of type null in %s on line %d
37+
3638
Notice: Trying to get property '' of non-object in %s on line %d
3739
bool(false)
3840
bool(true)

0 commit comments

Comments
 (0)