rfc:typehint_array_desctructuring

This is an old revision of the document!


PHP RFC: Type hints in array destructuring expressions

  • Version: 0.1
  • Date: 2020-04-16
  • Target Version: PHP 8.0
  • Author: Enno Woortmann, enno.woortmann@web.de
  • Status: Draft

Introduction

Adds the possibility to type hint values while using array or list destructuring expressions.

Motivation

When working with array destructuring expressions it's currently not possible to type hint the values from the array being destructured. As the destructured data often comes from various sources which may or may not be controlled by the scope executing the destructuring, a type hint inside the expression allows stricter boundaries for the processed array:

$data = [42, 'Example', 2002];
[int $id, string $data, int $year] = $data;

Proposal

This proposal adds a new syntax to add type hints to array destructuring expressions. The type hints behave identically to the type hints processed for function calls (compare https://wiki.php.net/rfc/scalar_type_hints_v5). This includes a controllability of the behaviour using the strict_types directive:

declare(strict_types=0);
 
// as we have disabled strict_types this is ok
[int $now, int $future] = ["2020", "2021"];
declare(strict_types=1);
 
// as we have enabled strict_types this will lead to an error
[int $now, int $future] = [2020, "2021"];
// Catchable fatal error: element 2 of array destructuring expression must be of type integer, string given
// type hinting an assiciative array destructuring
["now" => int $now, "future" => int $future] = ["now" => 2020, "future" => 2021];
 
// type hinting a nested array destructuring
[
    "2020s" => [
        "now" => int $now,
        "future" => int $future,
    ]
] = [
    "2020s" => [
        "now" => 2020,
        "future" => 2021,
    ],
    "2030s" => [
        "far away" => 2039,
    ],
];
 
// type hinting in a foreach loop
$years = [["now", 2020], ["future", 2021]];
foreach ($years as [string $description, int $year]) {
    // ...
}

Additionally to the examples above which all use scalar type hints also object type hints are possible:

foreach ($objectList as [DateTime $creationTime, MyObject $object]) {
    // ...
}

All of the examples above also work with the list() syntax.

Backward Incompatible Changes

None

Proposed PHP Version(s)

Next PHP version (target 8.0)

RFC Impact

To SAPIs

None

To Existing Extensions

None

To Opcache

Maybe, help needed

Open Issues

Proposed Voting Choices

As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC.

Patches and Tests

tbd

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Rejected Features

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

rfc/typehint_array_desctructuring.1587031240.txt.gz · Last modified: 2020/04/16 10:00 by wol-soft