Re: String enums & __toString()
| From: | Levi Morrison | Date: | Mon, 19 Aug 2024 15:10:06 +0000 |
| Subject: | Re: String enums & __toString() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125047@lists.php.net to get a copy of this message | ||
On Fri, Aug 16, 2024 at 1:43 PM John Coggeshall <john@coggeshall.org> wrote:
>
> Hey all,
>
> I poked around a bit in the Enum RFCs and I could find (maybe I missed) what's up with
> this:
>
> enum Foo:string implements \Stringable
> {
> case FOO = 'bar';
>
> public function __toString() {
> return $this->value;
> }
> }
>
> PHP Fatal error: Enum Foo cannot include magic method __toString
>
> It seems to me that when the enum is a string it is entirely reasonable to expect __toString()
> to work here and support the \Stringable interface..
>
> Is there a reason I'm missing as to why this is problematic in the engine that's been
> documented somewhere, or is this an oversight we can look at correcting?
>
> John
As others have said, there are definite objections to
auto-implementing string conversions. I think that opt-in string
conversions are sufficiently different that you would have a shot at
an RFC passing. No guarantees about that, of course!