On Wed, Nov 10, 2021 at 10:13 PM Jeremy Mikola <jmik...@gmail.com> wrote:

>
>
> On Tue, Nov 9, 2021 at 4:30 AM Nikita Popov <nikita....@gmail.com> wrote:
>
>>
>> In
>> https://github.com/php/php-src/commit/a551b083073ea08f8fc53b0e1a6380b6de26cc6b
>> I've added a hack to add the string return type if it is missing and thus
>> make the signature compatible with the interface. That should address the
>> immediate compatibility issue.
>>
>
> Thanks for the quick fix.
>
> A related question that came up, and is most likely unique to ext-mongodb,
> follows. Many of our classes with toString() methods also implement a
> corresponding interface with a toString() method. For example:
>
>  * https://www.php.net/manual/en/class.mongodb-bson-binary.php
>  * https://www.php.net/manual/en/class.mongodb-bson-binaryinterface.php
>
> I'm in the process of adding explicit return type info to _all_ of our
> toString() arginfos (classes and interfaces), but a thought occurred to me
> that doing so may be a subtle BC break for userland classes implementing
> these interfaces, since an explicit string return type would then become
> necessary. But if I only modify our classes and leave our interfaces as-is,
> PHP rightfully reports an error because the class' toString() method cannot
> conform to both Stringable (with type info) and our interface (without type
> info) -- at least PHP versions before 8.1.0RC6.
>
> Reading the patch above, it looks like the automatic Stringable
> implementation only kicks in when the arginfo has no existing return type
> info. In that case, I think the only option to completely avoid a BC break
> would be to continue to leave our return type info omitted (on both our
> classes _and_ interfaces) and allow PHP 8.1+ to apply it automatically. Is
> that correct?
>

With the introduction of Stringable PHP also started automatically adding
the string result type to __toString(), specifically for compatibility with
the interface. As such, it should be safe to add the string return type
everywhere for PHP >= 8.0. (If you use stubs with legacy arginfo, that
would automatically give you the right behavior: types on PHP 8, no types
on old versions.)

Regards,
Nikita

Reply via email to