Skip to content

Commit d8f2034

Browse files
committed
Implement enums
RFC: https://wiki.php.net/rfc/enumerations Co-authored-by: Nikita Popov <[email protected]> Closes GH-6489.
1 parent 6f38a53 commit d8f2034

File tree

165 files changed

+4301
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+4301
-52
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ PHP 8.1 UPGRADE NOTES
119119
RFC: https://wiki.php.net/rfc/explicit_octal_notation
120120
. Added support for array unpacking with strings keys.
121121
RFC: https://wiki.php.net/rfc/array_unpacking_string_keys
122+
. Added support for enumerations.
123+
RFC: https://wiki.php.net/rfc/enumerations
122124

123125
- Curl:
124126
. Added CURLOPT_DOH_URL option.

Zend/Optimizer/zend_inference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,7 @@ static zend_always_inline int _zend_update_type_info(
34863486
break;
34873487
case ZEND_FETCH_CONSTANT:
34883488
case ZEND_FETCH_CLASS_CONSTANT:
3489-
UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_RESOURCE|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY, ssa_op->result_def);
3489+
UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY, ssa_op->result_def);
34903490
break;
34913491
case ZEND_STRLEN:
34923492
tmp = MAY_BE_LONG;

Zend/tests/enum/__call.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Enum __call
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __call(string $name, array $args)
10+
{
11+
return [$name, $args];
12+
}
13+
}
14+
15+
var_dump(Foo::Bar->baz('qux', 'quux'));
16+
17+
?>
18+
--EXPECT--
19+
array(2) {
20+
[0]=>
21+
string(3) "baz"
22+
[1]=>
23+
array(2) {
24+
[0]=>
25+
string(3) "qux"
26+
[1]=>
27+
string(4) "quux"
28+
}
29+
}

Zend/tests/enum/__callStatic.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Enum __callStatic
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
public static function __callStatic(string $name, array $args)
8+
{
9+
return [$name, $args];
10+
}
11+
}
12+
13+
var_dump(Foo::bar('baz', 'qux'));
14+
15+
?>
16+
--EXPECT--
17+
array(2) {
18+
[0]=>
19+
string(3) "bar"
20+
[1]=>
21+
array(2) {
22+
[0]=>
23+
string(3) "baz"
24+
[1]=>
25+
string(3) "qux"
26+
}
27+
}

Zend/tests/enum/__class__.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Enum __CLASS__
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function printClass()
10+
{
11+
echo __CLASS__ . "\n";
12+
}
13+
}
14+
15+
Foo::Bar->printClass();
16+
17+
?>
18+
--EXPECT--
19+
Foo

Zend/tests/enum/__function__.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Enum __FUNCTION__
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function printFunction()
10+
{
11+
echo __FUNCTION__ . "\n";
12+
}
13+
}
14+
15+
Foo::Bar->printFunction();
16+
17+
?>
18+
--EXPECT--
19+
printFunction

Zend/tests/enum/__get.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Enum __get
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __get(string $name)
10+
{
11+
return '__get';
12+
}
13+
}
14+
15+
?>
16+
--EXPECTF--
17+
Fatal error: Enum may not include __get in %s on line %d

Zend/tests/enum/__invoke.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Enum __invoke
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __invoke(...$args)
10+
{
11+
return $args;
12+
}
13+
}
14+
15+
var_dump((Foo::Bar)('baz', 'qux'));
16+
17+
?>
18+
--EXPECT--
19+
array(2) {
20+
[0]=>
21+
string(3) "baz"
22+
[1]=>
23+
string(3) "qux"
24+
}

Zend/tests/enum/__isset.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Enum __isset
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __isset($property) {
10+
return true;
11+
}
12+
}
13+
14+
?>
15+
--EXPECTF--
16+
Fatal error: Enum may not include __isset in %s on line %d

Zend/tests/enum/__method__.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Enum __METHOD__
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function printMethod()
10+
{
11+
echo __METHOD__ . "\n";
12+
}
13+
}
14+
15+
Foo::Bar->printMethod();
16+
17+
?>
18+
--EXPECT--
19+
Foo::printMethod

0 commit comments

Comments
 (0)