This is an old revision of the document!

PHP RFC: Type casting in array destructuring expressions


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


This proposal aims at type casts when working with data from a untyped source (eg. iterating over a CSV file, XML without a XSD defining the types of the elements). This data can often be handled in an elegant way using the existing capabilities of array destructuring. While simple variable assignments can be casted during the assignment this isn't possible during assignments via array destructuring. This proposal adds a new syntax for casting in array assignments. An example when handling a CSV file may look like:

$handle = fopen('test.csv', 'r');
while (($data = fgetcsv($handle)) !== false) {
    [(int) $id, (string) $data, (int) $year] = $data;
    // ...


While a simple variable assignment is able to execute a type cast this possibility isn't present for array or list destructuring expressions. Instead all values are assigned as they are:

// simple assignment with a cast
$now = (int) "2020";
// array destructuring without cast
[$now, $future] = ["2020", "2021"];

If the values stored in $a and $b after the array destructuring shall be casted each value must be casted manually afterwards:

[$now, $future] = ["2020", "2021"];
// either touch each value manually, use array_map or any other implementation approach to cast the values
$now = (int) $now;
$future = (int) $future;

This RFC proposes the possibility to cast the values inside the destructuring expression:

// destructuring and casting of a numeric array
[(int) $now, (int) $future] = ["2020", "2021"];
// destructuring and casting of an assiciative array
["now" => (int) $now, "future" => (int) $future] = ["now" => "2020", "future" => "2021"];
// destructuring and casting of a nested array
    "2020s" => [
        "now" => (int) $now,
        "future" => (int) $future
] = [
    "2020s" => [
        "now" => "2020",
        "future" => "2021"
    "2030s" => [
        "far away" => "2039"
// destructuring and casting in a foreach loop
$years = [["now", "2020"], ["future", "2021"]];
foreach ($years as [$description, (int) $year]) {
    // ...

While examples where all values should be casted to the identical type may be solved reasonably elegant with solutions like array_map it get's more difficult if the casts should cover various types:

// destructuring and casting with various types
["address" => (bool) $hasAddress, "floor" => (int) $floor] = ["address" => "My adress", "floor" => "3"];

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

Backward Incompatible Changes


Proposed PHP Version(s)

Next PHP version (target 8.0)

RFC Impact



To Existing Extensions


To Opcache

Implementation uses existing functions to compile the code. So existing Opcache implementations for assignments and castings are used.

Open Issues


Regular type checks

During the discussion especially the idea of regular type checks instead of castings came up:

$years = ["2020", "2021"];
[int $now, int $future] = $years;

As a regular type check depends on the declare strict_types directive concerning the casting feature (strict_types=0 would result in an implicit cast similar to the proposed feature while strict_types=1 would result in a type error when the provided data doesn't match the type check) a regular type check covers different use cases than the proposed casting feature. (Also see future scopes)

Future Scope

Future scopes may include type casts during reference assignments which lead to a cast of the referenced variable (compare https://wiki.php.net/rfc/list_reference_assignment for reference assignments without casts):

// simple reference assignment cast
$now = "2020";
$now2 = (int) &$now;
// reference assignment cast combined with array destructuring
$years = ["2020", "2021"];
[(int) &$now, (int) &$future] = $years;

Future scopes may include strict type casts:

// simple strict assignment cast
$now = "2020";
$now2 = (!int) $now;
// simple strict assignment cast combined with array destructuring
$years = ["2020", "2021"];
[(!int) $now, (!int) $future] = $years;

Future scopes may include regular type checks which depend on strict_types directive:

$years = ["2020", "2021"];
[int $now, int $future] = $years;

Proposed Voting Choices

Vote will require 2/3 majority.

Patches and Tests

The parser is already able to parse the syntax and requires no changes. The patch adds a change in the compile process in zend_compile_list_assign to be able to handle casting AST elements.



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)


Links to external references, discussions or RFCs

Rejected Features

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

rfc/typecast_array_desctructuring.1586338011.txt.gz · Last modified: 2020/04/08 09:26 by wol-soft