Skip to content

Commit d574df6

Browse files
rjhdbynikic
authored andcommitted
1 parent d5943f5 commit d574df6

19 files changed

+120
-52
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ PHP 7.4 UPGRADE NOTES
348348
$a ? $b : ($c ? $d : $e)
349349

350350
RFC: https://wiki.php.net/rfc/ternary_associativity
351+
. The array and string offset access syntax using curly braces is deprecated.
352+
Use $str[$idx] instead of $str{$idx}.
353+
RFC: https://wiki.php.net/rfc/deprecate_curly_braces_array_access
351354
. Unbinding $this of a non-static method through a combination of
352355
ReflectionMethod::getClosure() and closure rebinding is deprecated. Doing
353356
so is equivalent to calling a non-static method statically, which has been

Zend/tests/036.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ $a{function() { }} = 1;
88

99
?>
1010
--EXPECTF--
11+
12+
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d
13+
1114
Warning: Illegal offset type in %s on line %d
1215

1316
Warning: Illegal offset type in %s on line %d

Zend/tests/bug71572.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Bug #71572: String offset assignment from an empty string inserts null byte
44
<?php
55

66
$str = "abc";
7-
var_dump($str{0} = "");
8-
var_dump($str{1} = "");
9-
var_dump($str{3} = "");
10-
var_dump($str{10} = "");
7+
var_dump($str[0] = "");
8+
var_dump($str[1] = "");
9+
var_dump($str[3] = "");
10+
var_dump($str[10] = "");
1111
var_dump($str);
1212
?>
1313
==DONE==

Zend/tests/constant_expressions_coalesce.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ Constant expressions with null coalescing operator ??
55

66
const A = [1 => [[]]];
77

8+
// should produce deprecation notices
9+
const D_1 = null ?? A[1]{'undefined'}['index'] ?? 1;
10+
const D_2 = null ?? A['undefined']{'index'} ?? 2;
11+
const D_3 = null ?? A[1]{0}{2} ?? 3; // 2 deprecation notices
12+
const D_4 = A[1]{0} ?? 4;
13+
814
const T_1 = null ?? A[1]['undefined']['index'] ?? 1;
915
const T_2 = null ?? A['undefined']['index'] ?? 2;
1016
const T_3 = null ?? A[1][0][2] ?? 3;
1117
const T_4 = A[1][0][2] ?? 4;
1218
const T_5 = null ?? __LINE__;
1319
const T_6 = __LINE__ ?? "bar";
1420

21+
var_dump(D_1);
22+
var_dump(D_2);
23+
var_dump(D_3);
24+
var_dump(D_4);
25+
1526
var_dump(T_1);
1627
var_dump(T_2);
1728
var_dump(T_3);
@@ -31,6 +42,21 @@ var_dump((new class { public $var = A[1][0][2] ?? 4; })->var);
3142

3243
?>
3344
--EXPECTF--
45+
46+
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d
47+
48+
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d
49+
50+
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d
51+
52+
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d
53+
54+
Deprecated: Array and string offset access syntax with curly braces is deprecated in %s line %d
55+
int(1)
56+
int(2)
57+
int(3)
58+
array(0) {
59+
}
3460
int(1)
3561
int(2)
3662
int(3)

Zend/tests/str_offset_004.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ $str = "abcdefghijklmno";
88
$i = 3;
99
$j = -4;
1010

11-
$str{2} = 'C';
11+
$str[2] = 'C';
1212
var_dump($str);
1313

14-
$str{$i} = 'Z';
14+
$str[$i] = 'Z';
1515
var_dump($str);
1616

17-
$str{-5} = 'P';
17+
$str[-5] = 'P';
1818
var_dump($str);
1919

20-
$str{$j} = 'Q';
20+
$str[$j] = 'Q';
2121
var_dump($str);
2222

23-
$str{-20} = 'Y';
23+
$str[-20] = 'Y';
2424
var_dump($str);
2525

26-
$str{-strlen($str)} = strtoupper($str{0}); /* An exotic ucfirst() ;) */
26+
$str[-strlen($str)] = strtoupper($str[0]); /* An exotic ucfirst() ;) */
2727
var_dump($str);
2828

29-
$str{20} = 'N';
29+
$str[20] = 'N';
3030
var_dump($str);
3131

32-
$str{-2} = 'UFO';
32+
$str[-2] = 'UFO';
3333
var_dump($str);
3434

35-
$str{-$i} = $str{$j*2};
35+
$str[-$i] = $str[$j*2];
3636
var_dump($str);
3737
?>
3838
--EXPECTF--

Zend/zend_ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ ZEND_API int ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_c
714714
zval_ptr_dtor_nogc(&op1);
715715
ret = FAILURE;
716716
} else {
717-
zend_fetch_dimension_const(result, &op1, &op2, (ast->attr == ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
717+
zend_fetch_dimension_const(result, &op1, &op2, (ast->attr & ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
718718

719719
zval_ptr_dtor_nogc(&op1);
720720
zval_ptr_dtor_nogc(&op2);

Zend/zend_compile.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,10 @@ static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node)
23712371

23722372
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
23732373
{
2374+
if (ast->attr == ZEND_DIM_ALTERNATIVE_SYNTAX) {
2375+
zend_error(E_DEPRECATED, "Array and string offset access syntax with curly braces is deprecated");
2376+
}
2377+
23742378
zend_ast *var_ast = ast->child[0];
23752379
zend_ast *dim_ast = ast->child[1];
23762380
zend_op *opline;
@@ -8745,7 +8749,7 @@ void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
87458749
case ZEND_AST_COALESCE:
87468750
/* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
87478751
if (ast->child[0]->kind == ZEND_AST_DIM) {
8748-
ast->child[0]->attr = ZEND_DIM_IS;
8752+
ast->child[0]->attr |= ZEND_DIM_IS;
87498753
}
87508754
zend_eval_const_expr(&ast->child[0]);
87518755

@@ -8799,9 +8803,14 @@ void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
87998803
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
88008804
}
88018805

8806+
if (ast->attr & ZEND_DIM_ALTERNATIVE_SYNTAX) {
8807+
ast->attr &= ~ZEND_DIM_ALTERNATIVE_SYNTAX; /* remove flag to avoid duplicate warning */
8808+
zend_error(E_DEPRECATED, "Array and string offset access syntax with curly braces is deprecated");
8809+
}
8810+
88028811
/* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
8803-
if (ast->attr == ZEND_DIM_IS && ast->child[0]->kind == ZEND_AST_DIM) {
8804-
ast->child[0]->attr = ZEND_DIM_IS;
8812+
if (ast->attr & ZEND_DIM_IS && ast->child[0]->kind == ZEND_AST_DIM) {
8813+
ast->child[0]->attr |= ZEND_DIM_IS;
88058814
}
88068815

88078816
zend_eval_const_expr(&ast->child[0]);

Zend/zend_compile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,8 @@ void zend_assert_valid_class_name(const zend_string *const_name);
924924
#define ZEND_SEND_BY_REF 1u
925925
#define ZEND_SEND_PREFER_REF 2u
926926

927-
#define ZEND_DIM_IS 1
927+
#define ZEND_DIM_IS (1 << 0) /* isset fetch needed for null coalesce */
928+
#define ZEND_DIM_ALTERNATIVE_SYNTAX (1 << 1) /* deprecated curly brace usage */
928929

929930
#define IS_CONSTANT_UNQUALIFIED 0x010
930931
#define IS_CONSTANT_CLASS 0x080 /* __CLASS__ in trait */

Zend/zend_language_parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ callable_variable:
11551155
| constant '[' optional_expr ']'
11561156
{ $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
11571157
| dereferencable '{' expr '}'
1158-
{ $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1158+
{ $$ = zend_ast_create_ex(ZEND_AST_DIM, ZEND_DIM_ALTERNATIVE_SYNTAX, $1, $3); }
11591159
| dereferencable T_OBJECT_OPERATOR property_name argument_list
11601160
{ $$ = zend_ast_create(ZEND_AST_METHOD_CALL, $1, $3, $4); }
11611161
| function_call { $$ = $1; }

ext/bz2/tests/005.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $data = bzcompress($string);
2121
$data2 = bzcompress($string, 1, 10);
2222

2323
$data3 = $data2;
24-
$data3{3} = 0;
24+
$data3[3] = 0;
2525

2626
var_dump(bzdecompress());
2727
var_dump(bzdecompress(1,1,1));

0 commit comments

Comments
 (0)