rfc:to-array
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:to-array [2020/02/04 13:02] – Update status to under discussion stevenwadejr | rfc:to-array [2020/02/11 13:57] (current) – Bump the version number now that it's under discussion and some feedback has been addressed stevenwadejr | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: __toArray() ====== | ====== PHP RFC: __toArray() ====== | ||
- | * Version: | + | * Version: |
* Date: 2019-08-28 | * Date: 2019-08-28 | ||
* Author: Steven Wade, stevenwadejr@gmail.com | * Author: Steven Wade, stevenwadejr@gmail.com | ||
Line 44: | 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 106: | Line 95: | ||
<code php> | <code php> | ||
- | print_r(array_keys($person)); | + | print_r( |
+ | | ||
+ | ); | ||
// Output | // Output | ||
Line 116: | 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 124: | Line 153: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | :?: Help needed | + | None |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |
rfc/to-array.txt · Last modified: 2020/02/11 13:57 by stevenwadejr