File: gh17713.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (169 lines) | stat: -rw-r--r-- 4,516 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
--TEST--
GH-17713: getRawValue(), setRawValue(), setRawValueWithoutLazyInitialization() may call hooks
--FILE--
<?php

#[AllowDynamicProperties]
class Base {
    public $publicProp = 'Base::$publicProp';
    private $privateProp = 'Base::$privateProp';
    public $hookedProp {
        get {
            echo __METHOD__, "\n";
            return $this->hookedProp;
        }
        set {
            echo __METHOD__, "\n";
            $this->hookedProp = $value;
        }
    }
    public $virtualProp {
        get {
            echo __METHOD__, "\n";
            return __METHOD__;
        }
        set {
            echo __METHOD__, "\n";
        }
    }
}

class Test extends Base {
    public $publicProp {
        get {
            echo __FUNCTION__, "\n";
            return $this->publicProp;
        }
        set {
            echo __FUNCTION__, "\n";
            $this->publicProp = $value;
        }
    }
    private $privateProp = 'Test::$privateProp' {
        get {
            echo __FUNCTION__, "\n";
            return $this->privateProp;
        }
        set {
            echo __FUNCTION__, "\n";
            $this->privateProp = $value;
        }
    }
    public $dynamicProp {
        get {
            echo __FUNCTION__, "\n";
            return $this->dynamicProp;
        }
        set {
            echo __FUNCTION__, "\n";
            $this->dynamicProp = $value;
        }
    }
    public $hookedProp {
        get {
            echo __METHOD__, "\n";
            return $this->hookedProp;
        }
        set {
            echo __METHOD__, "\n";
            $this->hookedProp = $value;
        }
    }
    public $virtualProp {
        get {
            echo __METHOD__, "\n";
            return $this->virtualProp;
        }
        set {
            echo __METHOD__, "\n";
            $this->virtualProp = $value;
        }
    }
    public static $changedProp = 'Test::$changedProp';
}

function test($scope, $class, $prop) {
    printf(
        "# Accessing %s->%s from scope %s\n",
        $class,
        $prop,
        is_object($scope) ? get_class($scope) : $scope,
    );

    $propertyReflection = new ReflectionProperty($scope, $prop);
    $object = new $class();
    try {
        $propertyReflection->setRawValue($object, 42);
    } catch (Throwable $e) {
        echo $e->getMessage(), "\n";
    }
    try {
        var_dump($propertyReflection->getRawValue($object));
    } catch (Throwable $e) {
        echo $e->getMessage(), "\n";
    }

    try {
        $propertyReflection->setRawValueWithoutLazyInitialization($object, 43);
    } catch (Throwable $e) {
        echo $e->getMessage(), "\n";
    }
    try {
        var_dump($propertyReflection->getRawValue($object));
    } catch (Throwable $e) {
        echo $e->getMessage(), "\n";
    }
    if ($prop === 'privateProp') {
        printf(
            "# Accessing %s->%s again from scope %s\n",
            $class,
            $prop,
            $class,
        );
        $propertyReflection = new ReflectionProperty($class, $prop);
        var_dump($propertyReflection->getRawValue($object));
    }
}

test(Base::class, Test::class, 'publicProp');
test(Base::class, Test::class, 'privateProp');
test(Base::class, Test::class, 'hookedProp');
test(Base::class, Test::class, 'virtualProp');
test(Base::class, Base::class, 'virtualProp');

$scope = new Base();
$scope->dynamicProp = 'Base::$dynamicProp';
$scope->changedProp = 'Base::$dynamicProp';

test($scope, Test::class, 'dynamicProp');
test($scope, Test::class, 'changedProp');

?>
--EXPECT--
# Accessing Test->publicProp from scope Base
int(42)
int(43)
# Accessing Test->privateProp from scope Base
int(42)
int(43)
# Accessing Test->privateProp again from scope Test
string(18) "Test::$privateProp"
# Accessing Test->hookedProp from scope Base
int(42)
int(43)
# Accessing Test->virtualProp from scope Base
int(42)
int(43)
# Accessing Base->virtualProp from scope Base
Must not write to virtual property Base::$virtualProp
Must not read from virtual property Base::$virtualProp
Can not use setRawValueWithoutLazyInitialization on virtual property Base::$virtualProp
Must not read from virtual property Base::$virtualProp
# Accessing Test->dynamicProp from scope Base
int(42)
int(43)
# Accessing Test->changedProp from scope Base
May not use setRawValue on static properties
May not use getRawValue on static properties
Can not use setRawValueWithoutLazyInitialization on static property Test::$changedProp
May not use getRawValue on static properties