Skip to content

Commit 4f8e081

Browse files
kocsismatemoliatabwoebi
committed
Implement typed class constants
RFC: https://wiki.php.net/rfc/typed_class_constants Co-Authored-By: Ben <[email protected]> Co-Authored-By: Bob Weinand <[email protected]>
1 parent 735edd1 commit 4f8e081

32 files changed

+554
-18
lines changed

Zend/tests/enum/name-property.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ array(1) {
3333
string(3) "Bar"
3434
array(2) {
3535
[0]=>
36-
object(ReflectionProperty)#3 (2) {
36+
object(ReflectionProperty)#4 (2) {
3737
["name"]=>
3838
string(4) "name"
3939
["class"]=>
4040
string(6) "IntFoo"
4141
}
4242
[1]=>
43-
object(ReflectionProperty)#4 (2) {
43+
object(ReflectionProperty)#5 (2) {
4444
["name"]=>
4545
string(5) "value"
4646
["class"]=>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Typed class constants (diamond error with self)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const self CONST1 = C;
7+
}
8+
9+
try {
10+
define("C", new A());
11+
} catch (Error $exception) {
12+
echo $exception->getMessage() . "\n";
13+
}
14+
?>
15+
--EXPECT--
16+
Undefined constant "C"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Typed class constants (incompatible inheritance; simple)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const int CONST1 = 1;
7+
}
8+
9+
class B extends A {
10+
public const string CONST1 = 'a';
11+
}
12+
?>
13+
--EXPECTF--
14+
Fatal error: Declaration of B::CONST1 must be compatible with A::CONST1 in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Typed class constants (incompatible inheritance; missing type in child)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const int CONST1 = 1;
7+
}
8+
9+
class B extends A {
10+
public const CONST1 = 0;
11+
}
12+
?>
13+
--EXPECTF--
14+
Fatal error: Declaration of B::CONST1 must be compatible with A::CONST1 in %s on line %d
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Typed class constants (inheritance success)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const CONST1 = 1;
7+
public const CONST2 = 1;
8+
public const mixed CONST3 = 1;
9+
public const iterable CONST4 = [];
10+
}
11+
12+
class B extends A {
13+
public const int CONST1 = 0;
14+
public const mixed CONST2 = 0;
15+
public const mixed CONST3 = 0;
16+
public const array CONST4 = [];
17+
}
18+
19+
var_dump(B::CONST1);
20+
var_dump(B::CONST2);
21+
var_dump(B::CONST3);
22+
var_dump(B::CONST4);
23+
?>
24+
--EXPECT--
25+
int(0)
26+
int(0)
27+
int(0)
28+
array(0) {
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
Typed class constants (inheritance success - object types)
3+
--FILE--
4+
<?php
5+
class S implements Stringable {
6+
public function __toString() {
7+
return "";
8+
}
9+
}
10+
11+
class Z extends S {}
12+
13+
class A {
14+
public const object CONST1 = S;
15+
public const S CONST2 = S;
16+
public const S|Stringable CONST3 = S;
17+
public const S CONST4 = S;
18+
public const ?S CONST5 = S;
19+
}
20+
21+
class B extends A {
22+
public const S CONST1 = Z;
23+
public const Z CONST2 = Z;
24+
public const S CONST3 = Z;
25+
public const S&Stringable CONST4 = Z;
26+
public const (S&Stringable)|null CONST5 = Z;
27+
}
28+
29+
define("S", new S());
30+
define("Z", new Z());
31+
32+
var_dump(B::CONST1);
33+
var_dump(B::CONST2);
34+
var_dump(B::CONST3);
35+
var_dump(B::CONST4);
36+
var_dump(B::CONST5);
37+
?>
38+
--EXPECTF--
39+
object(Z)#%d (%d) {
40+
}
41+
object(Z)#%d (%d) {
42+
}
43+
object(Z)#%d (%d) {
44+
}
45+
object(Z)#%d (%d) {
46+
}
47+
object(Z)#%d (%d) {
48+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Typed class constants (inheritance; private constants)
3+
--FILE--
4+
<?php
5+
class A {
6+
private const int CONST1 = 1;
7+
}
8+
9+
class B extends A {
10+
public const string CONST1 = 'a';
11+
}
12+
13+
var_dump(B::CONST1);
14+
?>
15+
--EXPECT--
16+
string(1) "a"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Typed class constants (type mismatch; simple)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const string CONST1 = 1;
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use int as value for class constant A::CONST1 of type string 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+
Typed class constants (type mismatch; runtime simple)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const int CONST1 = C;
7+
}
8+
9+
define("C", "c");
10+
11+
try {
12+
var_dump(A::CONST1);
13+
} catch (TypeError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
16+
?>
17+
--EXPECT--
18+
Cannot assign string to class constant A::CONST1 of type int
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Typed class constants (type mismatch; runtime object)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const string CONST1 = C;
7+
}
8+
9+
define('C', new stdClass);
10+
11+
try {
12+
var_dump(A::CONST1);
13+
} catch (TypeError $exception) {
14+
echo $exception->getMessage() . "\n";
15+
}
16+
?>
17+
--EXPECT--
18+
Cannot assign stdClass to class constant A::CONST1 of type string

0 commit comments

Comments
 (0)