File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
Zend/tests/readonly_props Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ Can override readonly property with attributes
3
+ --FILE--
4
+ <?php
5
+
6
+ #[Attribute]
7
+ class FooAttribute {}
8
+
9
+ class A {
10
+ public readonly int $ prop ;
11
+
12
+ public function __construct () {
13
+ $ this ->prop = 42 ;
14
+ }
15
+ }
16
+ class B extends A {
17
+ #[FooAttribute]
18
+ public readonly int $ prop ;
19
+ }
20
+
21
+ var_dump ((new ReflectionProperty (B::class, 'prop ' ))->getAttributes ()[0 ]->newInstance ());
22
+
23
+ ?>
24
+ --EXPECT--
25
+ object(FooAttribute)#1 (0) {
26
+ }
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ Visibility can change in readonly property
3
+ --FILE--
4
+ <?php
5
+
6
+ class A {
7
+ protected readonly int $ prop ;
8
+
9
+ public function __construct () {
10
+ $ this ->prop = 42 ;
11
+ }
12
+ }
13
+ class B extends A {
14
+ public readonly int $ prop ;
15
+ }
16
+
17
+ $ a = new A ();
18
+ try {
19
+ var_dump ($ a ->prop );
20
+ } catch (Error $ error ) {
21
+ echo $ error ->getMessage () . "\n" ;
22
+ }
23
+
24
+ $ b = new B ();
25
+ var_dump ($ b ->prop );
26
+
27
+ ?>
28
+ --EXPECT--
29
+ Cannot access protected property A::$prop
30
+ int(42)
You can’t perform that action at this time.
0 commit comments