Skip to content

Commit f408bce

Browse files
committed
- Set scope when copying a closure with a new this pointer.
1 parent a85d288 commit f408bce

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Zend/tests/closure_036.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Closure 036: Rebinding closure $this on property access, using scope
3+
--FILE--
4+
<?php
5+
6+
$instance = 0;
7+
8+
class Test {
9+
private $value = 42;
10+
function __construct() {
11+
global $instance;
12+
$this->instance = ++$instance;
13+
}
14+
}
15+
16+
$o = new Test;
17+
$o->func = function () {
18+
var_dump($this->value);
19+
};
20+
$func = $o->func;
21+
$func();
22+
23+
var_dump($instance);
24+
?>
25+
===DONE===
26+
--EXPECTF--
27+
int(42)
28+
int(1)
29+
===DONE===

Zend/zend_closures.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ ZEND_API zval* zend_closure_copy(zval *closure_obj, zval *this_ptr TSRMLS_DC) /*
134134
closure->this_ptr = this_ptr;
135135
if (this_ptr) {
136136
Z_ADDREF_P(this_ptr);
137+
closure->func.common.scope = Z_OBJCE_P(this_ptr);
138+
} else {
139+
closure->func.common.scope = NULL;
137140
}
138141
return closure_obj;
139142
}

0 commit comments

Comments
 (0)