This is an old revision of the document!
PHP RFC: Type casting in array destructuring expressions
- Version: 0.1
- Date: 2020-03-25
- Author: Enno Woortmann, enno.woortmann@web.de
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/typecast_array_desctructuring
Introduction
Adds the possibility to cast values while using array or list destructuring expressions.
Proposal
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]) { // ... }
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
Implementation uses existing functions to compile the code. So existing Opcache implementations for assignments and castings are used.
Open Issues
Future Scope
Future scopes may include type casts during reference assignments:
// 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;
Proposed Voting Choices
Vote will require 2/3 majority.
Patches and Tests
Implementation
After the project is implemented, this section should contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)
References
Links to external references, discussions or RFCs
Rejected Features
Keep this updated with features that were discussed on the mail lists.