Skip to content

Commit 07c81e5

Browse files
committed
Merge pull request #220 from stesie/fluent-setters
Retain object identity on 'return $this'
2 parents 21c8bd2 + d445929 commit 07c81e5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/return_this_basic.phpt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Test V8::executeString() : return $this (aka fluent setters)
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
8+
class Foo {
9+
private $foo;
10+
private $bar;
11+
12+
public function setFoo($value)
13+
{
14+
$this->foo = $value;
15+
return $this;
16+
}
17+
18+
public function setBar($value)
19+
{
20+
$this->bar = $value;
21+
return $this;
22+
}
23+
}
24+
25+
$v8 = new V8Js();
26+
$v8->theFoo = new Foo();
27+
28+
$v8->executeString(<<<EOJS
29+
var a = PHP.theFoo.setFoo(23);
30+
var b = a.setBar(42);
31+
32+
var_dump(PHP.theFoo === a);
33+
var_dump(PHP.theFoo === b);
34+
EOJS
35+
);
36+
37+
var_dump($v8->theFoo);
38+
39+
?>
40+
===EOF===
41+
--EXPECTF--
42+
bool(true)
43+
bool(true)
44+
object(Foo)#%d (2) {
45+
["foo":"Foo":private]=>
46+
int(23)
47+
["bar":"Foo":private]=>
48+
int(42)
49+
}
50+
===EOF===

v8js_object_export.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ static void v8js_call_php_func(zval *value, zend_class_entry *ce, zend_function
156156
} else {
157157
v8js_terminate_execution(isolate);
158158
}
159+
} else if (retval_ptr == value) {
160+
// special case: "return $this"
161+
return_value = info.Holder();
159162
} else if (retval_ptr != NULL) {
160163
return_value = zval_to_v8js(retval_ptr, isolate TSRMLS_CC);
161164
}

0 commit comments

Comments
 (0)