Skip to content

Commit ef11e1f

Browse files
committed
Add object prop binding shorthand
1 parent 653a10a commit ef11e1f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Object pattern matching
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function __construct(public $a, public $b) {}
8+
}
9+
10+
var_dump(new Foo(1, 2) is Foo { $a, b: 2 });
11+
var_dump($a);
12+
var_dump(new Foo(1, 2) is Foo { a: 3, $b });
13+
var_dump($b);
14+
15+
?>
16+
--EXPECTF--
17+
bool(true)
18+
int(1)
19+
bool(false)
20+
21+
Warning: Undefined variable $b in %s on line %d
22+
NULL

Zend/zend_language_parser.y

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,10 @@ non_empty_object_pattern_element_list:
13381338

13391339
object_pattern_element:
13401340
T_STRING ':' pattern { $$ = zend_ast_create(ZEND_AST_OBJECT_PATTERN_ELEMENT, $1, $3); }
1341+
| binding_pattern {
1342+
zend_string *name = zend_string_copy(zend_ast_get_str($1->child[0]));
1343+
$$ = zend_ast_create(ZEND_AST_OBJECT_PATTERN_ELEMENT, zend_ast_create_zval_from_str(name), $1);
1344+
}
13411345
;
13421346

13431347
or_pattern:

0 commit comments

Comments
 (0)