Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions Zend/tests/enum/__toString.phpt

This file was deleted.

18 changes: 18 additions & 0 deletions Zend/tests/enum/toString/backed_enum_value.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum with __toString() magic method - backed enum value accessible
--FILE--
<?php

enum Foo: string {
case Bar = "Baz";

public function __toString() {
return __CLASS__ . '::' . $this->name . ' = ' . $this->value;
}
}

echo Foo::Bar;

?>
--EXPECT--
Foo::Bar = Baz
29 changes: 29 additions & 0 deletions Zend/tests/enum/toString/magic_interface.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Enum with __toString() magic method - Stringable interface added
--FILE--
<?php

enum Foo {
case Bar;

public function __toString() {
return $this->name . ' is a case of the ' . __CLASS__ . ' enum';
}
}

function printStringable(Stringable $s) {
echo $s;
}

var_dump(class_implements(Foo::class));
printStringable(Foo::Bar);

?>
--EXPECT--
array(2) {
["UnitEnum"]=>
string(8) "UnitEnum"
["Stringable"]=>
string(10) "Stringable"
}
Bar is a case of the Foo enum
18 changes: 18 additions & 0 deletions Zend/tests/enum/toString/magic_method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum supports __toString() magic method
--FILE--
<?php

enum Foo {
case Bar;

public function __toString() {
return $this->name . ' is a case of the ' . __CLASS__ . ' enum';
}
}

echo Foo::Bar;

?>
--EXPECT--
Bar is a case of the Foo enum
18 changes: 18 additions & 0 deletions Zend/tests/enum/toString/param_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum with __toString() magic method - param validation
--FILE--
<?php

enum Foo {
case Bar;

public function __toString(mixed $arg): string {
return '';
}
}

echo Foo::Bar;

?>
--EXPECTF--
Fatal error: Method Foo::__toString() cannot take arguments in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/enum/toString/return_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum with __toString() magic method - return validation
--FILE--
<?php

enum Foo {
case Bar;

public function __toString(): int {
return 5;
}
}

echo Foo::Bar;

?>
--EXPECTF--
Fatal error: Foo::__toString(): Return type must be string when declared in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/enum/toString/visibility_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Enum with __toString() magic method - visibility validation
--FILE--
<?php

enum Foo {
case Bar;

private function __toString(): string {
return '';
}
}

echo Foo::Bar;

?>
--EXPECTF--
Warning: The magic method Foo::__toString() must have public visibility in %s on line %d

Fatal error: Access level to Foo::__toString() must be public (as in class Stringable) in %s on line %d
3 changes: 1 addition & 2 deletions Zend/zend_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void zend_verify_enum_properties(const zend_class_entry *ce)

static void zend_verify_enum_magic_methods(const zend_class_entry *ce)
{
// Only __get, __call and __invoke are allowed
// Only __get, __call, __toString, and __invoke are allowed

ZEND_ENUM_DISALLOW_MAGIC_METHOD(constructor, "__construct");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(destructor, "__destruct");
Expand All @@ -89,7 +89,6 @@ static void zend_verify_enum_magic_methods(const zend_class_entry *ce)
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__set, "__set");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unset, "__unset");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__isset, "__isset");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__tostring, "__toString");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__debugInfo, "__debugInfo");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__serialize, "__serialize");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unserialize, "__unserialize");
Expand Down