rfc:stringable
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:stringable [2020/01/15 16:15] – created nicolasgrekas | rfc:stringable [2020/08/01 23:38] (current) – RFC was implemented carusogabriel | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 0.9 | * Version: 0.9 | ||
* Date: 2020-01-15 | * Date: 2020-01-15 | ||
- | * Author: Nicolas Grekas, | + | * Author: Nicolas Grekas, |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This PR introduces a new Stringable interface that can be added to implementations of the __toString() method. | + | |
+ | This RFC introduces a new '' | ||
It has two goals: | It has two goals: | ||
- | 1. allow using "string|Stringable" | + | - allow using '' |
- | 2. provide a forward upgrade path from PHP 7 to 8 | + | |
===== Proposal ===== | ===== Proposal ===== | ||
- | This goal is to allow using the string|Stringable union type in PHP 8, to accept both strings and objects that implement __toString() | + | Goal 1. is to allow using the '' |
- | + | ||
- | On purpose, there is no corresponding is_stringable() function, because that would require defining what happens when e.g. an int is passed to this function, and there are no simple answers to this concern. | + | |
- | On purpose also, for BC, classes | + | Classes |
- | Such classes wouldn' | + | |
- | 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:// |
- | + | ||
- | Once a polyfill becomes widely available (I would personally add one to symfony/ | + | |
Here is the stub declaration of the interface: | Here is the stub declaration of the interface: | ||
+ | <PHP> | ||
interface Stringable | interface Stringable | ||
{ | { | ||
| | ||
} | } | ||
+ | </ | ||
- | Because it adds the string return type, this interface has the potential to force a BC break on any existing libraries that want to adopt it. | + | Because it adds the '' |
- | In order to ease forward and backward compatibility, | + | In order to ease forward and backward compatibility, |
- | This way, code moving to PHP8 won't be forced to add the return type explicitly (which would break BC on their side), and code in PHP < 8 can adopt a polyfill interface immediately (one that doesn' | + | This way, code moving to PHP8 won't be forced to add the return type explicitly (which would break BC on their side), and code in PHP < 8 can adopt a polyfill interface immediately (one that doesn' |
- | For reference, here are some annotations in Symfony, added by contributions from real-world use cases and that currently cannot be expressed precisely enough using any union types in PHP 8: | + | Providing an easy forward-path is the second goal of this RFC. |
- | https:// | + | |
+ | For reference, | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | Code that declare a symbol named "Stringable" | + | Codes that declare a symbol named '' |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 48: | Line 48: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
==== To Existing Extensions ==== | ==== To Existing Extensions ==== | ||
- | Extensions will need to add the `string` return type when they declare | + | Extensions will need to declare both the interface and the '' |
===== Open Issues ===== | ===== Open Issues ===== | ||
- | Make sure there are no open issues when the vote starts! | + | none |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | List existing areas/ | + | Declaring this interface is not mandatory to benefit from the magic of the '' |
- | + | ||
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | + | |
===== Future Scope ===== | ===== Future Scope ===== | ||
- | This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | + | void |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Accept the proposed interface: | + | yes/no |
+ | |||
+ | ===== Vote ===== | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 78: | Line 82: | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed | + | |
+ | - Adding a new '' | ||
+ | - Consistently, | ||
+ | - it has been proposed [[https:// |
rfc/stringable.txt · Last modified: 2020/08/01 23:38 by carusogabriel