Skip to content

Commit 38beb44

Browse files
jiripudilTimWolla
andauthored
[RFC] Extend #[\Override] to target properties (#19061)
RFC: https://wiki.php.net/rfc/override_properties Co-authored-by: Tim Düsterhus <[email protected]>
1 parent c3bee21 commit 38beb44

27 files changed

+449
-19
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PHP NEWS
1212
. The backtick operator as an alias for shell_exec() has been deprecated.
1313
(timwolla)
1414
. Returning null from __debugInfo() has been deprecated. (DanielEScherzer)
15+
. Support #[\Override] on properties. (Jiří Pudil)
1516

1617
- Curl:
1718
. The curl_close() function has been deprecated. (DanielEScherzer)

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ PHP 8.5 UPGRADE NOTES
178178
RFC: https://wiki.php.net/rfc/pipe-operator-v3
179179
. Constructor property promotion can now be used for final properties.
180180
RFC: https://wiki.php.net/rfc/final_promotion
181+
. #[\Override] can now be applied to properties.
182+
RFC: https://wiki.php.net/rfc/override_properties
181183

182184
- Curl:
183185
. Added support for share handles that are persisted across multiple PHP
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
#[\Override]: Properties
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
public mixed $i { get; }
8+
}
9+
10+
interface II extends I {
11+
#[\Override]
12+
public mixed $i { get; }
13+
}
14+
15+
class P {
16+
public mixed $p1;
17+
public mixed $p2;
18+
}
19+
20+
class PP extends P {
21+
#[\Override]
22+
public mixed $p1;
23+
public mixed $p2;
24+
}
25+
26+
class C extends PP implements I {
27+
#[\Override]
28+
public mixed $i;
29+
#[\Override]
30+
public mixed $p1;
31+
public mixed $p2;
32+
public mixed $c;
33+
}
34+
35+
echo "Done";
36+
37+
?>
38+
--EXPECT--
39+
Done
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
#[\Override]: Properties: No parent class.
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
#[\Override]
8+
public mixed $c;
9+
}
10+
11+
echo "Done";
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: C::$c has #[\Override] attribute, but no matching parent property exists in %s on line %d
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
#[\Override]: Properties: No parent class, but child implements matching interface.
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
public mixed $i { get; }
8+
}
9+
10+
class P {
11+
#[\Override]
12+
public mixed $i;
13+
}
14+
15+
class C extends P implements I {}
16+
17+
echo "Done";
18+
19+
?>
20+
--EXPECTF--
21+
Fatal error: P::$i has #[\Override] attribute, but no matching parent property exists in %s on line %d
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
#[\Override]: Properties: No parent class, but child implements matching interface (2).
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
public mixed $i { get; }
8+
}
9+
10+
class C extends P implements I {}
11+
12+
class P {
13+
#[\Override]
14+
public mixed $i;
15+
}
16+
17+
echo "Done";
18+
19+
?>
20+
--EXPECTF--
21+
Fatal error: P::$i has #[\Override] attribute, but no matching parent property exists in %s on line %d
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
#[\Override]: Properties: No parent interface.
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
#[\Override]
8+
public mixed $i { get; }
9+
}
10+
11+
interface II extends I {}
12+
13+
14+
class C implements II {
15+
public mixed $i;
16+
}
17+
18+
class C2 implements I {
19+
public mixed $i;
20+
}
21+
22+
echo "Done";
23+
24+
?>
25+
--EXPECTF--
26+
Fatal error: I::$i has #[\Override] attribute, but no matching parent property exists in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
#[\Override]: Properties: On trait.
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
#[\Override]
8+
public mixed $t;
9+
}
10+
11+
echo "Done";
12+
13+
?>
14+
--EXPECT--
15+
Done
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
#[\Override]: Properties: On used trait without parent property.
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
#[\Override]
8+
public mixed $t;
9+
}
10+
11+
class Foo {
12+
use T;
13+
}
14+
15+
echo "Done";
16+
17+
?>
18+
--EXPECTF--
19+
Fatal error: Foo::$t has #[\Override] attribute, but no matching parent property exists in %s on line %d
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
#[\Override]: Properties: On used trait with interface property.
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
#[\Override]
8+
public mixed $i;
9+
}
10+
11+
interface I {
12+
public mixed $i { get; }
13+
}
14+
15+
class Foo implements I {
16+
use T;
17+
}
18+
19+
echo "Done";
20+
21+
?>
22+
--EXPECT--
23+
Done

0 commit comments

Comments
 (0)