rfc:array-to-string

This is an old revision of the document!


Improve array to string conversion

  • Version: 1.1
  • Creation date: 2015-01-10
  • Last modification date : 2015-01-11
  • Author: François Laupretre, francois@tekwire.net
  • Status: Under discussion

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'.

I consider this as a half-backed solution : we raise an error, which is not a 'real' error (an E_WARNING would be) and we return a useless string (anybody relying on the 'Array' value, please call me :). Off topic: I am personally against the existence of E_NOTICE and E_STRICT, which are, IMO, half-backed solutions too.

The only possible justification I imagine for the current behavior is to have stopped a flame war or for (wrong) BC reasons.

Now, the release of a new major version is the occasion to get rid of problematic behaviors we have been pulling behind us for years.

I think of two possible alternative and opposite behaviors :

1st alternative: deprecate

Here, we consider that array-to-string conversion was never actively supported nor encouraged and that it is time to disable it definitely.

As noone ever agreed on a 'standard' way to convert an array to a string, the only clean solution is to really disable it.

Array-to-string conversion will be considered like object-to-string with no 'toString' method: any conversion attempt generates a 'fatal catchable error'. Then, if the program is still alive, the conversion returns 'Array' as before.

We could go through a migration phase with an E_DEPRECATED but the 5.7 release was rejected and we already raise an E_NOTICE. If 5.7 is revived, we'll replace the E_NOTICE with E_DEPRECATED but, otherwise, this phase can probably be skipped.

2nd alternative: full support

Here, we decide that array-to-string conversion is a valid concept. We define a standard algorithm to perform the conversion, fix it in stone and encourage C and PHP code to use it with no restriction.

Of course, the first step is to remove the E_NOTICE.

The 2nd step is the choice of the conversion algorithm. We could for instance convert the array to a recursive concatenation of every non-array elements of the array tree, each of them converted to a string (a recursive implode() with no 'glue'). Array elements would be taken in array order, keys would be ignored.

Existing rules for non-array types to string conversions would be applied to all non-array members of the input array.

Backward Incompatible Changes

If we deprecate array-to-string, we consider that array to string conversion is an error. We just don't tolerate this error anymore. The BC break is 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.

If we add full support, the removal of E_NOTICE has no impact. And, as all of us probably agree that anybody relying on the 'Array' string should choose another profession, the overall BC break is minimal.

Proposed PHP Version(s)

PHP 7

RFC Impact

Touches every explicit or implicit array-to-string conversions, in C or PHP code.

Open Issues

None (yet)

Unaffected PHP Functionality

No other zval type conversion should be impacted.

Future Scope

None

Proposed Voting Choices

Required majority : 2/3, and even probably more :)

Patches and Tests

Implementation is easy.

Patches and tests will be written as soon as we seem to agree on something.

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

Rejected Features

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

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