1--TEST--
2Lazy objects: RFC example 006
3--FILE--
4<?php
5
6class MyClass {
7    public $propA;
8    public $propB;
9}
10
11// Creating two lazy objects. The initializer of $object1 causes the initialization
12// of $object2, which fails.
13
14$reflector = new ReflectionClass(MyClass::class);
15
16$object2 = $reflector->newLazyGhost(function ($object2) {
17    $object2->propB = 'value';
18    throw new \Exception('initializer exception');
19});
20$reflector->getProperty('propA')->setRawValueWithoutLazyInitialization($object2, 'object-2');
21
22$object1 = $reflector->newLazyGhost(function ($object1) use ($object2) {
23    $object1->propB = 'updated';
24    $object1->propB = $object2->propB;
25});
26$reflector->getProperty('propA')->setRawValueWithoutLazyInitialization($object1, 'object-1');
27
28// Both objects are uninitalized at this point
29
30var_dump($object1);
31var_dump($object2);
32
33try {
34    var_dump($object1->propB);
35} catch (Exception $e) {
36    echo $e->getMessage(), "\n";
37}
38
39// The state of both objects is unchanged
40
41var_dump($object1);
42var_dump($object2);
43
44?>
45==DONE==
46--EXPECTF--
47lazy ghost object(MyClass)#%d (1) {
48  ["propA"]=>
49  string(8) "object-1"
50}
51lazy ghost object(MyClass)#%d (1) {
52  ["propA"]=>
53  string(8) "object-2"
54}
55initializer exception
56lazy ghost object(MyClass)#%d (1) {
57  ["propA"]=>
58  string(8) "object-1"
59}
60lazy ghost object(MyClass)#%d (1) {
61  ["propA"]=>
62  string(8) "object-2"
63}
64==DONE==
65