Define Stringable with toString():string method by nicolas-grekas · Pull Request #5088 · php/php-src

This PR is opened for the purpose of comparison with #5083

It adds a new interface:

interface Stringable
{
   public function toString(): string;
}

Implementing this interface triggers the same magic behavior as implementing __toString().

Compared to #5083, the new method has no double underscore as a prefix, which means it can more easily be used as part of public APIs.

Yet, it requires a more invasive change in the engine, and leads to duplicate __toString() and toString() methods for BC. Since adding a method named toString() can be done purely in userland, it appears that this approach might not be the preferred one for php-internals, which seeks for the lesser complexity.