rfc:to-array
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:to-array [2019/12/04 18:23] – stevenwadejr | rfc:to-array [2020/02/11 13:04] – Update error message comment stevenwadejr | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2019-08-28 | * Date: 2019-08-28 | ||
* Author: Steven Wade, stevenwadejr@gmail.com | * Author: Steven Wade, stevenwadejr@gmail.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | PHP contains many [[https:// | ||
This RFC proposes to add a new magic method called < | This RFC proposes to add a new magic method called < | ||
+ | |||
+ | PHP contains many [[https:// | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 43: | Line 44: | ||
<code php> | <code php> | ||
- | print_r($person); | ||
- | |||
- | // Output | ||
- | /* | ||
- | Array | ||
- | ( | ||
- | [name] => John Doe | ||
- | [email] => j.doe@example.com | ||
- | ) | ||
- | */ | ||
- | |||
$personArray = (array) $person; // casting triggers __toArray() | $personArray = (array) $person; // casting triggers __toArray() | ||
</ | </ | ||
==== What this is ==== | ==== What this is ==== | ||
- | The example above shows the method < | + | The example above shows the method < |
Similar to PHP's current implementation of < | Similar to PHP's current implementation of < | ||
Line 105: | Line 95: | ||
<code php> | <code php> | ||
- | print_r(array_keys($person)); | + | print_r( |
+ | | ||
+ | ); | ||
// Output | // Output | ||
Line 115: | Line 107: | ||
) | ) | ||
*/ | */ | ||
+ | </ | ||
+ | |||
+ | === Strict Types === | ||
+ | |||
+ | Automatic casting will not work when using strict types. | ||
+ | |||
+ | <code php> | ||
+ | declare(strict_types=1); | ||
+ | |||
+ | function bar(Person $person): array { | ||
+ | return $person; | ||
+ | } | ||
+ | |||
+ | bar($person); | ||
+ | |||
+ | function foo(array $person) { | ||
+ | var_dump($person); | ||
+ | } | ||
+ | |||
+ | foo($person); | ||
+ | </ | ||
+ | |||
+ | Manual casting within strict types will continue to work and is allowed. | ||
+ | |||
+ | <code php> | ||
+ | declare(strict_types=1); | ||
+ | |||
+ | function bar(Person $person): array { | ||
+ | return (array) $person; | ||
+ | } | ||
+ | |||
+ | bar($person); | ||
+ | |||
+ | function foo(array $person) { | ||
+ | var_dump($person); | ||
+ | } | ||
+ | |||
+ | foo((array) $person); // Allowed | ||
</ | </ | ||
Line 123: | Line 153: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | :?: Help needed | + | None |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 137: | Line 167: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
:?: Help needed | :?: Help needed | ||
+ | |||
+ | ===== Concerns ===== | ||
+ | |||
+ | A [[https:// | ||
+ | |||
+ | >As it currently stands, the array cast is the only operation capable of exposing object state without triggering any kind of access guards: it is very much required for anything that works with reflection and typed properties, and possibly the only operation in PHP that operates on state without some contraption intercepting its execution. | ||
+ | |||
+ | As a response to this concern, the new < | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 145: | Line 183: | ||
===== References ===== | ===== References ===== | ||
- | * PHP Manual: [[https:// | + | |
- | * PHP RFC: [[https:// | + | * PHP RFC: [[https:// |
- | * Initial idea and discussion: [[https:// | + | * Initial idea and discussion: [[https:// |
===== Rejected Features ===== | ===== Rejected Features ===== | ||
rfc/to-array.txt · Last modified: 2020/02/11 13:57 by stevenwadejr