summaryrefslogtreecommitdiff
path: root/ext/reflection/tests/ReflectionProperty_isInitialized.phpt
blob: 74e25252d1a0e9d7218da376cd3eedfce11e824d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
--TEST--
Test ReflectionProperty::isInitialized()
--FILE--
<?php

class A {
    public static ?string $ssv = null;
    public static ?string $ss;
    public static $s;
    public ?int $iv = null;
    public ?int $i;
    public $n;

    private int $p;
}

class B extends A { }

echo "Static properties:\n";
var_dump((new ReflectionProperty(A::class, 'ssv'))->isInitialized());
var_dump((new ReflectionProperty(A::class, 'ss'))->isInitialized());
var_dump((new ReflectionProperty(A::class, 's'))->isInitialized());

echo "Declared properties:\n";
$a = new A;
var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a));
var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a));
var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a));

echo "Declared properties after unset:\n";
unset($a->iv);
unset($a->i);
unset($a->n);
var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a));
var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a));
var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a));

echo "Dynamic properties:\n";
$a->d = null;
$rp = new ReflectionProperty($a, 'd');
var_dump($rp->isInitialized($a));
unset($a->d);
var_dump($rp->isInitialized($a));

echo "Visibility handling:\n";
$rp = new ReflectionProperty('A', 'p');
try {
    var_dump($rp->isInitialized($a));
} catch (ReflectionException $e) {
    echo $e->getMessage(), "\n";
}
$rp->setAccessible(true);
var_dump($rp->isInitialized($a));

echo "Object type:\n";
$rp = new ReflectionProperty('B', 'i');
var_dump($rp->isInitialized($a));

try {
    var_dump($rp->isInitialized(new stdClass));
} catch (ReflectionException $e) {
    echo $e->getMessage(), "\n";
}

try {
    var_dump($rp->isInitialized());
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}

class WithMagic {
    public $prop;
    public int $intProp;

    public function __isset($name) {
        echo "__isset($name)\n";
        return true;
    }

    public function __get($name) {
        echo "__get($name)\n";
        return "foobar";
    }
}

echo "Class with __isset:\n";
$obj = new WithMagic;
unset($obj->prop);
$rp = new ReflectionProperty('WithMagic', 'prop');
var_dump($rp->isInitialized($obj));
$rp = new ReflectionProperty('WithMagic', 'intProp');
var_dump($rp->isInitialized($obj));

?>
--EXPECT--
Static properties:
bool(true)
bool(false)
bool(true)
Declared properties:
bool(true)
bool(false)
bool(true)
Declared properties after unset:
bool(false)
bool(false)
bool(false)
Dynamic properties:
bool(true)
bool(false)
Visibility handling:
Cannot access non-public property A::$p
bool(false)
Object type:
bool(false)
Given object is not an instance of the class this property was declared in
ReflectionProperty::isInitialized(): Argument #1 ($object) must be provided for instance properties
Class with __isset:
bool(false)
bool(false)