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