rfc:array-to-string

This is an old revision of the document!


Improve array to string conversion

  • Version: 2.0
  • Creation date: 2015-01-10
  • Last modification date : 2015-02-23
  • Author: François Laupretre, francois@tekwire.net
  • Status: In voting phase

Introduction

Currently, array to string zval conversion raises an E_NOTICE and returns a string containing 'Array'.

This RFC considers this behavior as non-optimal and proposes improvements.

Proposal

As seen above, zval conversion from array to string, implicit or explicit, gives E_NOTICE and 'Array'.

We consider this as no more than a temporary half-backed solution : we raise a low-level error and we return a useless string (anybody writing code relying on the “Array” value, please call me :).

It was probably done this way as a compromise before making a definitive decision.

Now, the release of a new major version is the prefect occasion to make such decision.

Note: the RFC originally proposed, either to fully support the feature, or to deprecate it.
Following discussion, it was decided to only propose the option of deprecating it.
So the other option was removed, and can be consulted via older versions of the RFC.

1st alternative: deprecate

Here, we consider that array-to-string conversion was never actively supported nor encouraged, that, it most cases, its only effect is hiding bugs, and that it is time to disable it definitely.

Another argument is that there never was an agreement on the 'standard' way to convert an array to a string.

So, we propose that Array-to-string conversions are considered exactly the same as object-to-string with no '__toString' method: any conversion attempt generates an E_RECOVERABLE_ERROR. Then, if the program is still alive, the conversion returns 'Array' as before. The E_NOTICE message is suppressed. No more worries about hiding bugs with this logic, as masking such an error level is a strong user decision, definitely not the same as disabling E_NOTICE.

We could go through a migration phase using E_DEPRECATED, but we consider that, since an E_NOTICE is currently generated, it may be considered as a deprecation notice, and we can directly switch to E_RECOVERABLE_ERROR.

Another argument is that, while this one should not be very problematic, it is always better to introduce a BC break in a major version.

If 5.7 is revived, it will contain an interim version, with E_NOTICE replaced by E_DEPRECATED.

2nd alternative: full support

Removed

Backward Incompatible Changes

We consider that array to string conversion is an error. We just don't tolerate this error anymore. The potential BC breaks are important because a lot of PHP code runs in E_NOTICE-disabled environments and the 'Array' return value, while probably invalid, may easily go unnoticed.

In balance of this, we can consider that raising the error level in environments where E_NOTICE messages are disabled, will generally help discovering bugs that had remained unnoticed because the error level was hidden.

Actually, during the discussion, everyone converged towards the opinion that raising potential bugs is more important than the potential BC break.

Proposed PHP Version(s)

7.0

RFC Impact

Touches every explicit or implicit array-to-string conversion, whether called from extensions or in PHP code.

Open Issues

None

Unaffected PHP Functionality

No other type conversion is impacted.

Future Scope

None

Proposed Voting Choices

Basic Yes/No : Yes to replace E_NOTICE with E_RECOVERABLE_ERROR, No for no change.

array-to-string
Real name Yes No
aharvey (aharvey)  
ben (ben)  
brandon (brandon)  
bwoebi (bwoebi)  
danack (danack)  
datibbaw (datibbaw)  
diegopires (diegopires)  
duodraco (duodraco)  
francois (francois)  
frozenfire (frozenfire)  
guilhermeblanco (guilhermeblanco)  
gwynne (gwynne)  
hywan (hywan)  
ircmaxell (ircmaxell)  
jedibc (jedibc)  
jpauli (jpauli)  
jwage (jwage)  
kalle (kalle)  
kinncj (kinncj)  
krakjoe (krakjoe)  
laruence (laruence)  
lcobucci (lcobucci)  
leigh (leigh)  
lstrojny (lstrojny)  
mbeccati (mbeccati)  
mfischer (mfischer)  
mike (mike)  
mrook (mrook)  
nikic (nikic)  
olemarkus (olemarkus)  
patrickallaert (patrickallaert)  
pauloelr (pauloelr)  
pollita (pollita)  
rasmus (rasmus)  
rdohms (rdohms)  
santiagolizardo (santiagolizardo)  
sobak (sobak)  
stas (stas)  
till (till)  
treffynnon (treffynnon)  
tyrael (tyrael)  
yohgaki (yohgaki)  
yunosh (yunosh)  
zeev (zeev)  
Count: 34 10

Voting will close March 6th, 00:00 (UTC).

Required majority : 2/3 of voters.

Patches and Tests

Patch provided, included modifcation of existing tests.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

PR : https://github.com/php/php-src/pull/991

32 tests are broken by this change. The coresponding fixes are under way and not included in the PR.

Rejected Features

(Keep this updated with features that were discussed on the mail lists)

rfc/array-to-string.1424706240.txt.gz · Last modified: 2017/09/22 13:28 (external edit)