Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision |
rfc:stringable [2020/01/25 12:16] – nicolasgrekas | rfc:stringable [2020/02/12 18:34] – nicolasgrekas |
---|
* Version: 0.9 | * Version: 0.9 |
* Date: 2020-01-15 | * Date: 2020-01-15 |
* Author: Nicolas Grekas, nicolas.grekas+php@gmail.com | * Author: Nicolas Grekas, nicolasgrekas@php.net |
* Status: Under Discussion | * Status: In voting phase |
* First Published at: http://wiki.php.net/rfc/stringable | * First Published at: http://wiki.php.net/rfc/stringable |
| |
===== Introduction ===== | ===== Introduction ===== |
| |
This RFC introduces a new ''Stringable'' interface that can be added to classes that implement the ''%%__toString()%%'' method. | This RFC introduces a new ''Stringable'' interface that is automatically added to classes that implement the ''%%__toString()%%'' method. |
| |
It has two goals: | It has two goals: |
| |
===== Proposal ===== | ===== Proposal ===== |
Goal 1. is to allow using the ''string|Stringable'' union type in PHP 8, to accept both strings and objects that implement ''%%__toString()%%'' //and// declare this interface. This is critically missing currently, on codes that deal with stringable objects: they can't be made type-safe. | Goal 1. is to allow using the ''string|Stringable'' union type in PHP 8, to accept both strings and objects that implement ''%%__toString()%%''. This is critically missing currently, on codes that deal with stringable objects: they can't be made type-safe. |
| |
Classes that implement ''%%__toString()%%'' aren't required to declare the interface as that would prevent seamless forward/backward compatibility with PHP 7. | Classes that implement ''%%__toString()%%'' can also declare the interface explicitly. Classes that don't declare the interface explicitly will still declare it implicitly. This allows both forward compatibility and backward compatibility: using a polyfill, classes can declare the interface on PHP 7; and on PHP 8, classes that don't do so will still be compatible with the ''string|Stringable'' union type. |
| |
By being simple and without any magic capabilities on its own, this interface is trivially polyfilled on PHP < 8. | Once a polyfill becomes widely available (e.g. [[https://github.com/symfony/polyfill/pull/224|as part of symfony/polyfill-php80]]), code style checkers could be able to enforce declaring the interface when ''%%__toString()%%'' is declared explicitly, for people that prefer doing so. |
| |
Once a polyfill becomes widely available (e.g. [[https://github.com/symfony/polyfill/pull/224|as part of symfony/polyfill-php80]]), we can expect code style checkers to be able to enforce declaring the interface when ''%%__toString()%%'' is used. For projects that don't use cs checkers, they'll notice quickly that they missed adding the interface when their users will ask for it if they do. | |
| |
Here is the stub declaration of the interface: | Here is the stub declaration of the interface: |
===== RFC Impact ===== | ===== RFC Impact ===== |
==== To Existing Extensions ==== | ==== To Existing Extensions ==== |
Extensions will need to add the ''string'' return type when they declare ''%%__toString()%%'' methods if they want to adopt the new interface. | Extensions will need to declare both the interface and the ''string'' return type when they declare ''%%__toString()%%'' methods if they want to pass the ''string|Stringable'' union type. |
| |
===== Open Issues ===== | ===== Open Issues ===== |
| |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== |
Accept the proposed interface for PHP 8.0: yes/no | yes/no |
| |
| ===== Vote ===== |
| <doodle title="Adopt the Stringable interface as proposed in this RFC?" auth="nicolasgrekas" voteType="single" closed="false"> |
| * Yes |
| * No |
| </doodle> |
| |
===== Patches and Tests ===== | ===== Patches and Tests ===== |