8000 Implement enums · php/php-src@a0a35d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0a35d9

Browse files
committed
Implement enums
RFC: https://wiki.php.net/rfc/enumerations Co-authored-by: Larry Garfield <larry@garfieldtech.com> Closes GH-6489.
1 parent 6f38a53 commit a0a35d9

File tree

150 files changed

+3887
-55
lines changed

Some content is hidden

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

150 files changed

+3887
-55
lines changed

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 {
< D7AE /td>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

Zend/tests/enum/ast-dumper.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
Enum AST dumper
3+
--FILE--
4+
<?php
5+
6+
try {
7+
assert((function () {
8+
enum Foo {
9+
case Bar;
10+
}
11+
12+
#[EnumAttr]
13+
enum IntFoo: int {
14+
#[CaseAttr]
15+
case Bar = 1 << 0;
16+
case Baz = 1 << 1;
17+
18+
public function self() {
19+
return $this;
20+
}
21+
}
22+
23+
return false;
24+
})());
25+
} catch (Error $e) {
26+
echo $e->getMessage();
27+
}
28+
29+
?>
30+
--EXPECT--
31+
assert(function () {
32+
enum Foo {
33+
case Bar;
34+
}
35+
36+
#[EnumAttr]
37+
enum IntFoo: int {
38+
#[CaseAttr]
39+
case Bar = 1 << 0;
40+
case Baz = 1 << 1;
41+
public function self() {
42+
return $this;
43+
}
44+
45+
}
46+
47+
return false;
48+
}())

Zend/tests/enum/basic-methods.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Enum methods
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
case Baz;
9+
10+
public function dump() {
11+
var_dump($this);
12+
}
13+
}
14+
15+
Foo::Bar->dump();
16+
Foo::Baz->dump();
17+
18+
?>
19+
--EXPECT--
20+
enum(Foo::Bar)
21+
enum(Foo::Baz)

Zend/tests/enum/case-attributes.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Enum case attributes
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
7+
class EnumCaseAttribute {
8+
public function __construct(
9+
public string $value,
10+
) {}
11+
}
12+
13+
enum Foo {
14+
#[EnumCaseAttribute('Bar')]
15+
case Bar;
16+
}
17+
18+
var_dump((new \ReflectionClassConstant(Foo::class, 'Bar'))->getAttributes(EnumCaseAttribute::class)[0]->newInstance());
19+
20+
?>
21+
--EXPECT--
22+
object(EnumCaseAttribute)#1 (1) {
23+
["value"]=>
24+
string(3) "Bar"
25+
}

Zend/tests/enum/case-in-class.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Enum case in class
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
case Bar;
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Case can only be used in enums in %s on line %d

Zend/tests/enum/cases-refcount.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Enum cases increases refcount
3+
--FILE--
< 97AE div aria-hidden="true" class="position-absolute top-0 d-flex user-select-none DiffLineTableCellParts-module__comment-indicator--eI0hb">
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
}
9+
10+
function callCases() {
11+
Foo::cases();
12+
}
13+
14+
callCases();
15+
debug_zval_dump(Foo::Bar);
16+
17+
?>
18+
--EXPECT--
19+
object(Foo)#2 (1) refcount(2){
20+
["name"]=>
21+
string(3) "Bar" refcount(2)
22+
}

Zend/tests/enum/comparison.phpt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
Enum comparison
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
case Baz;
9+
}
10+
11+
$bar = Foo::Bar;
12+
$baz = Foo::Baz;
13+
14+
var_dump($bar === $bar);
15+
var_dump($bar == $bar);
16+
17+
var_dump($bar === $baz);
18+
var_dump($bar == $baz);
19+
20+
var_dump($baz === $bar);
21+
var_dump($baz == $bar);
22+
23+
var_dump($bar > $bar);
24+
var_dump($bar < $bar);
25+
var_dump($bar >= $bar);
26+
var_dump($bar <= $bar);
27+
28+
var_dump($bar > $baz);
29+
var_dump($bar < $baz);
30+
var_dump($bar >= $baz);
31+
var_dump($bar <= $baz);
32+
33+
var_dump($bar > true);
34+
var_dump($bar < true);
35+
var_dump($bar >= true);
36+
var_dump($bar <= true);
37+
38+
?>
39+
--EXPECT--
40+
bool(true)
41+
bool(true)
42+
bool(false)
43+
bool(false)
44+
bool(false)
45+
bool(false)
46+
bool(false)
47+
bool(false)
48+
bool(true)
49+
bool(true)
50+
bool(false)
51+
bool(false)
52+
bool(false)
53+
bool(false)
54+
bool(false)
55+
bool(false)
56+
bool(false)
57+
bool(false)

Zend/tests/enum/constant-aliases.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Enum constants can alias cases
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
const Baz = self::Bar;
9+
}
10+
11+
function test(Foo $var) {
12+
echo "works\n";
13+
}
14+
15+
test(Foo::Bar);
16+
test(Foo::Baz);
17+
18+
?>
19+
--EXPECT--
20+
works
21+
works

0 commit comments

Comments
 (0)
0