xref: /php-src/Zend/tests/lazy_objects/isLazy.phpt (revision 54a40f3b)
1--TEST--
2Lazy Objects: ReflectionProperty::isLazy()
3--FILE--
4<?php
5
6#[AllowDynamicProperties]
7class C {
8    public static $staticProp;
9    public int $typed;
10    public $untyped;
11    public $virtual {
12        get {}
13    }
14}
15
16function testProps(ReflectionClass $reflector, object $obj) {
17    foreach (['staticProp', 'typed', 'untyped', 'virtual', 'dynamic'] as $name) {
18        if ('dynamic' === $name) {
19            $tmp = new C();
20            $tmp->dynamic = 1;
21            $pr = new ReflectionProperty($tmp, $name);
22        } else {
23            $pr = $reflector->getProperty($name);
24        }
25        printf("%s: %d\n", $name, $pr->isLazy($obj));
26    }
27}
28
29$reflector = new ReflectionClass(C::class);
30
31print "# Ghost\n";
32
33$obj = $reflector->newLazyGhost(function () { });
34
35testProps($reflector, $obj);
36
37$pr = $reflector->getProperty('typed');
38$pr->skipLazyInitialization($obj);
39printf("typed (skipped): %d\n", $pr->isLazy($obj));
40
41print "# Initialized Ghost\n";
42
43$reflector->initializeLazyObject($obj);
44
45testProps($reflector, $obj);
46
47print "# Proxy\n";
48
49$obj = $reflector->newLazyProxy(function () {
50    return new C();
51});
52
53testProps($reflector, $obj);
54
55$pr = $reflector->getProperty('typed');
56$pr->skipLazyInitialization($obj);
57printf("typed (skipped prop): %d\n", $pr->isLazy($obj));
58
59print "# Initialized Proxy\n";
60
61$reflector->initializeLazyObject($obj);
62
63testProps($reflector, $obj);
64
65print "# Nested Proxy\n";
66
67$nested = new C();
68$obj = $reflector->newLazyProxy(function () use ($nested) {
69    return $nested;
70});
71$reflector->initializeLazyObject($obj);
72$reflector->resetAsLazyProxy($nested, function () {
73    return new C();
74});
75
76testProps($reflector, $obj);
77
78print "# Nested Proxy (nested initialized)\n";
79
80$nested = new C();
81$obj = $reflector->newLazyProxy(function () use ($nested) {
82    return $nested;
83});
84$reflector->initializeLazyObject($obj);
85$reflector->resetAsLazyProxy($nested, function () {
86    return new C();
87});
88$reflector->initializeLazyObject($nested);
89
90testProps($reflector, $obj);
91
92print "# Internal\n";
93
94$obj = (new DateTime())->diff(new DateTime());
95$reflector = new ReflectionClass(DateInterval::class);
96$pr = new ReflectionProperty($obj, 'y');
97printf("y: %d\n", $pr->isLazy($obj));
98
99?>
100--EXPECT--
101# Ghost
102staticProp: 0
103typed: 1
104untyped: 1
105virtual: 0
106dynamic: 0
107typed (skipped): 0
108# Initialized Ghost
109staticProp: 0
110typed: 0
111untyped: 0
112virtual: 0
113dynamic: 0
114# Proxy
115staticProp: 0
116typed: 1
117untyped: 1
118virtual: 0
119dynamic: 0
120typed (skipped prop): 0
121# Initialized Proxy
122staticProp: 0
123typed: 0
124untyped: 0
125virtual: 0
126dynamic: 0
127# Nested Proxy
128staticProp: 0
129typed: 1
130untyped: 1
131virtual: 0
132dynamic: 0
133# Nested Proxy (nested initialized)
134staticProp: 0
135typed: 0
136untyped: 0
137virtual: 0
138dynamic: 0
139# Internal
140y: 0
141