Skip to content

Commit

Permalink
Fix nullsafe operator on $this
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Aug 11, 2020
1 parent 42eda51 commit 227f1f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
25 changes: 25 additions & 0 deletions Zend/tests/nullsafe_operator/032.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Nullsafe operator on $this
--FILE--
<?php

class Test {
public $foo = 42;

public function method() {
var_dump($this?->foo);
var_dump($this?->bar());
}

public function bar() {
return 24;
}
}

$test = new Test;
$test->method();

?>
--EXPECT--
int(42)
int(24)
20 changes: 12 additions & 8 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2788,14 +2788,16 @@ static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t
zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
}
CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;

/* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
* check for a nullsafe access. */
} else {
zend_short_circuiting_mark_inner(obj_ast);
opline = zend_delayed_compile_var(&obj_node, obj_ast, type, 0);
zend_separate_if_call_and_write(&obj_node, obj_ast, type);
}

if (nullsafe) {
zend_emit_jmp_null(&obj_node);
if (nullsafe) {
zend_emit_jmp_null(&obj_node);
}
}

zend_compile_expr(&prop_node, prop_ast);
Expand Down Expand Up @@ -4347,13 +4349,15 @@ void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{
zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
}
CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;

/* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
* check for a nullsafe access. */
} else {
zend_short_circuiting_mark_inner(obj_ast);
zend_compile_expr(&obj_node, obj_ast);
}

if (nullsafe) {
zend_emit_jmp_null(&obj_node);
if (nullsafe) {
zend_emit_jmp_null(&obj_node);
}
}

zend_compile_expr(&method_node, method_ast);
Expand Down

0 comments on commit 227f1f1

Please sign in to comment.