Skip to content

Commit

Permalink
- MFH - Set scope when copying a closure with a new this pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
helly25 committed Jan 4, 2009
1 parent 08a2529 commit 8e74167
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Zend/tests/closure_036.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Closure 036: Rebinding closure $this on property access, using scope
--FILE--
<?php

$instance = 0;

class Test {
private $value = 42;
function __construct() {
global $instance;
$this->instance = ++$instance;
}
}

$o = new Test;
$o->func = function () {
var_dump($this->value);
};
$func = $o->func;
$func();

var_dump($instance);
?>
===DONE===
--EXPECTF--
int(42)
int(1)
===DONE===
3 changes: 3 additions & 0 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ ZEND_API zval* zend_closure_copy(zval *closure_obj, zval *this_ptr TSRMLS_DC) /*
closure->this_ptr = this_ptr;
if (this_ptr) {
Z_ADDREF_P(this_ptr);
closure->func.common.scope = Z_OBJCE_P(this_ptr);
} else {
closure->func.common.scope = NULL;
}
return closure_obj;
}
Expand Down

0 comments on commit 8e74167

Please sign in to comment.