Skip to content

Commit 762f8b9

Browse files
kocsismatemoliatabwoebi
committed
Typed constants
Co-Authored-By: moliata <[email protected]> Co-Authored-By: Bob Weinand <[email protected]>
1 parent 64d9080 commit 762f8b9

30 files changed

+446
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Typed class constants (declaration; simple)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const null A = null;
7+
public const false B = false;
8+
public const true C = true;
9+
public const bool D = true;
10+
public const int E = 0;
11+
public const float F = 3.14;
12+
public const string G = "";
13+
public const array H = [];
14+
public const array|string I = "";
15+
public const array|string|null J = null;
16+
}
17+
18+
var_dump(A::A);
19+
var_dump(A::B);
20+
var_dump(A::C);
21+
var_dump(A::D);
22+
var_dump(A::E);
23+
var_dump(A::F);
24+
var_dump(A::G);
25+
var_dump(A::H);
26+
var_dump(A::I);
27+
var_dump(A::J);
28+
?>
29+
--EXPECT--
30+
NULL
31+
bool(false)
32+
bool(true)
33+
bool(true)
34+
int(0)
35+
float(3.14)
36+
string(0) ""
37+
array(0) {
38+
}
39+
string(0) ""
40+
NULL
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; compile-time)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const string A = 1;
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use int as value for class constant A::A of type string 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+
Typed class constants (type mismatch; runtime)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const string A = A;
7+
}
8+
9+
define('A', 1);
10+
11+
echo A::A;
12+
?>
13+
--EXPECTF--
14+
Fatal error: Uncaught TypeError: Cannot assign int to class constant A::A of type string in %s:%d
15+
Stack trace:
16+
#0 {main}
17+
thrown 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 (inheritance; simple)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const int A = 1;
7+
}
8+
9+
class B extends A {
10+
public const string A = 'a';
11+
}
12+
?>
13+
--EXPECTF--
14+
Fatal error: Declaration of B::A must be compatible with A::A 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 (inheritance; missing type in child)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const int A = 1;
7+
}
8+
9+
class B extends A {
10+
public const A = 0;
11+
}
12+
?>
13+
--EXPECTF--
14+
Fatal error: Declaration of B::A must be compatible with A::A in %s on line %d
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 not allowed; object)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const object A = 1;
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Class constant A::A cannot have type object in %s on line %d
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 not allowed; void)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const void A = 1;
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Class constant A::A cannot have type void in %s on line %d
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 not allowed; callable)
3+
--FILE--
4+
<?php
5+
class A {
6+
public const callable A = 1;
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Class constant A::A cannot have type callable in %s on line %d
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 A = 1;
7+
}
8+
9+
class B extends A {
10+
public const string A = 'a';
11+
}
12+
13+
echo B::A;
14+
?>
15+
--EXPECT--
16+
a

0 commit comments

Comments
 (0)