Skip to content

Commit a5360e8

Browse files
kocsismatenikic
andcommitted
Add support for final class constants
RFC: https://wiki.php.net/rfc/final_class_const Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>
1 parent c6d4f60 commit a5360e8

28 files changed

+340
-94
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ PHP 8.1 UPGRADE NOTES
6767
. Added support for intersection types.
6868
They cannot be combined with union types.
6969
RFC: https://wiki.php.net/rfc/pure-intersection-types
70+
. Added support for the final modifier for class constants.
71+
RFC: https://wiki.php.net/rfc/final_class_const
7072

7173
- Fileinfo:
7274
. The fileinfo functions now accept and return, respectively, finfo objects

Zend/tests/bug49472.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ class FooBar extends Foo implements ia {
2323
new FooBar;
2424

2525
?>
26-
--EXPECTF--
27-
Fatal error: Cannot inherit previously-inherited or override constant c from interface ia in %s on line %d
26+
===DONE===
27+
--EXPECT--
28+
===DONE===
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Class constants support the final modifier
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final const A = "foo";
9+
final public const B = "foo";
10+
}
11+
12+
?>
13+
--EXPECT--
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Interface constants inherited from other interfaces can be redeclared
3+
--FILE--
4+
<?php
5+
6+
interface I1
7+
{
8+
const C = 1;
9+
}
10+
11+
interface I2
12+
{
13+
const C = 2;
14+
}
15+
16+
interface I3 extends I1, I2
17+
{
18+
const C = 3;
19+
}
20+
21+
?>
22+
--EXPECT--
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Class constants cannot be inherited from both a class and an interface
3+
--FILE--
4+
<?php
5+
6+
class C
7+
{
8+
const C = 1;
9+
}
10+
11+
interface I
12+
{
13+
const C = 1;
14+
}
15+
16+
class C2 extends C implements I
17+
{
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
Fatal error: Class C2 inherits both C::C and I::C, which is ambiguous in %s on line %d
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Interface constants cannot be inherited from other interfaces
3+
--FILE--
4+
<?php
5+
6+
interface I1
7+
{
8+
const C = 1;
9+
}
10+
11+
interface I2
12+
{
13+
const C = 2;
14+
}
15+
16+
interface I3 extends I1, I2
17+
{
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
Fatal error: Class I3 inherits both I1::C and I2::C, which is ambiguous in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Final class constants cannot be overridden
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final const A = "foo";
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
const A = "bar";
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Bar::A cannot override final constant Foo::A in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Private class constants cannot be final
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
private final const A = "foo";
9+
}
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Private constant Foo::A cannot be final as it is not visible to other classes in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Interface constants can be overridden directly
3+
--FILE--
4+
<?php
5+
6+
interface I
7+
{
8+
const X = 1;
9+
}
10+
11+
class C implements I
12+
{
13+
const X = 2;
14+
}
15+
16+
?>
17+
--EXPECT--
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Final interface constants cannot be overridden directly
3+
--FILE--
4+
<?php
5+
6+
interface I
7+
{
8+
final public const X = 1;
9+
}
10+
11+
class C implements I
12+
{
13+
const X = 2;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: C::X cannot override final constant I::X in %s on line %d

0 commit comments

Comments
 (0)