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 [2020/02/04 12:54] – 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:// | ||
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) ===== | ||
Line 143: | Line 172: | ||
A [[https:// | A [[https:// | ||
- | >As it currently stands, the array cast is the only operation capable of | + | >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. |
- | 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 < | As a response to this concern, the new < |
rfc/to-array.txt · Last modified: 2020/02/11 13:57 by stevenwadejr