Enum could not be converted to string

I need help, please. Maybe it’s a bug or a feature request.

I’m getting the following error when parsing an enum to xml document:

“App\Model\Enum\AutoAppointmentBooking could not be converted to string”

It occures in this line (CakePHP 5.1.1):
CORE/src/Utility/Xml.php at line 344

   $child->appendChild(new DOMText((string)$value));

Here is the enum:

enum AutoAppointmentBooking: int
{
    case OFF = 0;
    case ON = 1;
    case ON_OUTSIDE_BUSINESS_HOURS = 2;

}

When I try to add __toString() method to the enum, I get a fatal error:

“Fatal error: Enum App\Model\Enum\AutoAppointmentBooking cannot include magic method __toString”

How can I solve that?

You need to manually check if your value is a backed enum ($value instanceof \BackedEnum) and then call $value->value instead of relying on PHP to magically convert your enum.

But that code is in the Xml Utility class in cakephp’s core library. Or what do you mean?

Ah I thought that was part of your code.

Seems like something, that needs to be changed in the core then.

Could you create an issue with an example so that we can fix that?

I did it… Thank you.

Best regards
Marc