1--TEST--
2Test ReflectionProperty::isInitialized()
3--FILE--
4<?php
5
6#[AllowDynamicProperties]
7class A {
8    public static ?string $ssv = null;
9    public static ?string $ss;
10    public static $s;
11    public ?int $iv = null;
12    public ?int $i;
13    public $n;
14
15    private int $p;
16}
17
18class B extends A { }
19
20echo "Static properties:\n";
21var_dump((new ReflectionProperty(A::class, 'ssv'))->isInitialized());
22var_dump((new ReflectionProperty(A::class, 'ss'))->isInitialized());
23var_dump((new ReflectionProperty(A::class, 's'))->isInitialized());
24
25echo "Declared properties:\n";
26$a = new A;
27var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a));
28var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a));
29var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a));
30
31echo "Declared properties after unset:\n";
32unset($a->iv);
33unset($a->i);
34unset($a->n);
35var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a));
36var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a));
37var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a));
38
39echo "Dynamic properties:\n";
40$a->d = null;
41$rp = new ReflectionProperty($a, 'd');
42var_dump($rp->isInitialized($a));
43unset($a->d);
44var_dump($rp->isInitialized($a));
45
46echo "Visibility handling:\n";
47$rp = new ReflectionProperty('A', 'p');
48var_dump($rp->isInitialized($a));
49var_dump($rp->isInitialized($a));
50
51echo "Object type:\n";
52$rp = new ReflectionProperty('B', 'i');
53var_dump($rp->isInitialized($a));
54
55try {
56    var_dump($rp->isInitialized(new stdClass));
57} catch (ReflectionException $e) {
58    echo $e->getMessage(), "\n";
59}
60
61try {
62    var_dump($rp->isInitialized());
63} catch (TypeError $e) {
64    echo $e->getMessage(), "\n";
65}
66
67class WithMagic {
68    public $prop;
69    public int $intProp;
70
71    public function __isset($name) {
72        echo "__isset($name)\n";
73        return true;
74    }
75
76    public function __get($name) {
77        echo "__get($name)\n";
78        return "foobar";
79    }
80}
81
82echo "Class with __isset:\n";
83$obj = new WithMagic;
84unset($obj->prop);
85$rp = new ReflectionProperty('WithMagic', 'prop');
86var_dump($rp->isInitialized($obj));
87$rp = new ReflectionProperty('WithMagic', 'intProp');
88var_dump($rp->isInitialized($obj));
89
90?>
91--EXPECT--
92Static properties:
93bool(true)
94bool(false)
95bool(true)
96Declared properties:
97bool(true)
98bool(false)
99bool(true)
100Declared properties after unset:
101bool(false)
102bool(false)
103bool(false)
104Dynamic properties:
105bool(true)
106bool(false)
107Visibility handling:
108bool(false)
109bool(false)
110Object type:
111bool(false)
112Given object is not an instance of the class this property was declared in
113ReflectionProperty::isInitialized(): Argument #1 ($object) must be provided for instance properties
114Class with __isset:
115bool(false)
116bool(false)
117