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 [2019/08/29 04:15] – Add placeholders sections 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 | ||
- | * 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 37: | Line 38: | ||
} | } | ||
- | $person = new Person(' | + | $person = new Person(' |
</ | </ | ||
Line 43: | Line 44: | ||
<code php> | <code php> | ||
- | print_r($person); // calls __toArray() | + | $personArray = (array) $person; // casting triggers __toArray() |
+ | </ | ||
+ | |||
+ | ==== What this is ==== | ||
+ | The example above shows the method < | ||
+ | |||
+ | Similar to PHP's current implementation of < | ||
+ | |||
+ | **Type Hinting** | ||
+ | |||
+ | <code php> | ||
+ | function foo(array $person) { | ||
+ | var_dump($person); | ||
+ | } | ||
+ | |||
+ | // Output | ||
+ | /* | ||
+ | array(2) { | ||
+ | [" | ||
+ | string(8) "John Doe" | ||
+ | [" | ||
+ | string(17) " | ||
+ | } | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | **Return Type** | ||
+ | |||
+ | <code php> | ||
+ | function bar(Person $person): array { | ||
+ | return $person; | ||
+ | } | ||
+ | |||
+ | var_dump(bar($person)); | ||
+ | |||
+ | // Output | ||
+ | /* | ||
+ | array(2) { | ||
+ | [" | ||
+ | string(8) "John Doe" | ||
+ | [" | ||
+ | string(17) " | ||
+ | } | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | **array_* | ||
+ | |||
+ | The array operating functions listed on the [[https:// | ||
+ | |||
+ | <code php> | ||
+ | print_r( | ||
+ | array_keys($person) | ||
+ | ); | ||
// Output | // Output | ||
Line 49: | Line 103: | ||
Array | Array | ||
( | ( | ||
- | [name] => Steven Wade | + | [0] => first |
- | [email] => stevenwadejr@gmail.com | + | [1] => email |
) | ) | ||
*/ | */ | ||
+ | </ | ||
- | $personArray | + | === Strict Types === |
+ | |||
+ | Automatic casting will not work when using strict types. | ||
+ | |||
+ | <code php> | ||
+ | declare(strict_types=1); | ||
+ | |||
+ | function bar(Person $person): | ||
+ | return $person; | ||
+ | } | ||
+ | |||
+ | bar($person); // Throws an error: " | ||
+ | |||
+ | function foo(array | ||
+ | var_dump($person); | ||
+ | } | ||
+ | |||
+ | foo($person); // Throws an error: " | ||
</ | </ | ||
- | ==== What this is ==== | + | Manual casting within strict types will continue to work and is allowed. |
- | Describe what this RFC covers and what internal functions trigger | + | |
+ | <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 | ||
+ | </ | ||
==== What this is not ==== | ==== What this is not ==== | ||
- | Discuss what the RFC does not cover (accessing and setting values as you would in a normal array. | + | This proposal |
+ | |||
+ | Array functions that operate on an array by reference such as < | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | What breaks, and what is the justification for it? | + | None |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 71: | Line 160: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
==== To SAPIs ==== | ==== To SAPIs ==== | ||
- | Describe the impact to CLI, Development web server, embedded PHP etc. | + | :?: Help needed |
==== To Existing Extensions ==== | ==== To Existing Extensions ==== | ||
- | Will existing extensions be affected? | + | :?: Help needed |
==== To Opcache ==== | ==== To Opcache ==== | ||
- | It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP. | + | :?: Help needed |
- | Please explain how you have verified your RFC's compatibility with opcache. | + | ===== Concerns ===== |
- | ==== New Constants ==== | + | A [[https:// |
- | Describe any new constants so they can be accurately and comprehensively explained | + | |
- | ==== php.ini Defaults ==== | + | >As it currently stands, the array cast is the only operation capable of exposing object state without triggering |
- | If there are any php.ini settings then list: | + | |
- | * hardcoded default values | + | |
- | * php.ini-development values | + | |
- | * php.ini-production values | + | |
- | ===== Open Issues ===== | + | As a response to this concern, |
- | Make sure there are no open issues when the vote starts! | + | |
- | + | ||
- | ===== Unaffected PHP Functionality ===== | + | |
- | List existing areas/features of PHP that will not be changed by the RFC. | + | |
- | + | ||
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | + | |
- | + | ||
- | ===== Future Scope ===== | + | |
- | This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | + | |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Include these so readers know where you are heading and can discuss the proposed voting options. | + | Vote will require 2/3 majority |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Links to any external patches and tests go here. | + | No patch exists yet. A [[https:// |
- | + | ||
- | If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. | + | |
- | + | ||
- | Make it clear if the patch is intended to be the final patch, or is just a prototype. | + | |
- | + | ||
- | For changes affecting the core language, you should also provide a patch for the language specification. | + | |
- | + | ||
- | ===== Implementation ===== | + | |
- | After the project is implemented, | + | |
- | - 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 ===== | ===== References ===== | ||
- | Links to external references, discussions or RFCs | + | * PHP Manual: [[https:// |
+ | * PHP RFC: [[https:// | ||
+ | * Initial idea and discussion: [[https:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed on the mail lists. | + |
rfc/to-array.1567052122.txt.gz · Last modified: 2019/08/29 04:15 by stevenwadejr