Skip to content

Commit 227f1f1

Browse files
committed
Fix nullsafe operator on $this
1 parent 42eda51 commit 227f1f1

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Zend/tests/nullsafe_operator/032.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Nullsafe operator on $this
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public $foo = 42;
8+
9+
public function method() {
10+
var_dump($this?->foo);
11+
var_dump($this?->bar());
12+
}
13+
14+
public function bar() {
15+
return 24;
16+
}
17+
}
18+
19+
$test = new Test;
20+
$test->method();
21+
22+
?>
23+
--EXPECT--
24+
int(42)
25+
int(24)

Zend/zend_compile.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,14 +2788,16 @@ static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t
27882788
zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
27892789
}
27902790
CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
2791+
2792+
/* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
2793+
* check for a nullsafe access. */
27912794
} else {
27922795
zend_short_circuiting_mark_inner(obj_ast);
27932796
opline = zend_delayed_compile_var(&obj_node, obj_ast, type, 0);
27942797
zend_separate_if_call_and_write(&obj_node, obj_ast, type);
2795-
}
2796-
2797-
if (nullsafe) {
2798-
zend_emit_jmp_null(&obj_node);
2798+
if (nullsafe) {
2799+
zend_emit_jmp_null(&obj_node);
2800+
}
27992801
}
28002802

28012803
zend_compile_expr(&prop_node, prop_ast);
@@ -4347,13 +4349,15 @@ void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{
43474349
zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
43484350
}
43494351
CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
4352+
4353+
/* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
4354+
* check for a nullsafe access. */
43504355
} else {
43514356
zend_short_circuiting_mark_inner(obj_ast);
43524357
zend_compile_expr(&obj_node, obj_ast);
4353-
}
4354-
4355-
if (nullsafe) {
4356-
zend_emit_jmp_null(&obj_node);
4358+
if (nullsafe) {
4359+
zend_emit_jmp_null(&obj_node);
4360+
}
43574361
}
43584362

43594363
zend_compile_expr(&method_node, method_ast);

0 commit comments

Comments
 (0)