This is an old revision of the document!

PHP RFC: __toArray()


PHP contains many magic methods that give a class greater control over its interaction with the language. The methods __serialize() and __unserialize() give a class control over how it is serialized, __clone() allows control over how self copies are made, and __toString() allows a class to control how it is represented when converted to a string.

This RFC proposes to add a new magic method called __toArray() to allow a class to control how it is represented when converted to an array.



class Person
    protected $name;
    protected $email;
    public $foo = 'bar';
    public function __construct(string $name, string $email)
        $this->name = $name;
        $this->email  = $email;
    public function __toArray()
        return [
            'name' => $this->name,
            'email'  => $this->email,
$person = new Person('John Doe', 'j.doe@example.com');

Example usage:

print_r($person); // calls __toArray()
// Output
    [name] => John Doe
    [email] => j.doe@example.com
$personArray = (array) $person; // casting triggers __toArray()

Use in Array Contexts

The example above shows the method __toArray() used in a type-casting context. This proposal would have objects implementing the __toArray() magic method called within any array context including type hinting and return types.

Similar to PHP's current implementation of __toString(), a copy of the given object's value as an array is made upon conversion.

Type Hinting

function foo(array $person) {

// Output
array(2) {
  string(8) "John Doe"
  string(17) "j.doe@example.com"

Return Type

function bar(Person $person): array {
    return $person;


// Output
array(2) {
  string(8) "John Doe"
  string(17) "j.doe@example.com"

What this is

Describe what this RFC covers and what internal functions trigger __toArray() (array_keys())

What this is not

Discuss what the RFC does not cover (accessing and setting values as you would in a normal array.

Backward Incompatible Changes

What breaks, and what is the justification for it?

Proposed PHP Version(s)

Next PHP version (target 8.0)

RFC Impact


Describe the impact to CLI, Development web server, embedded PHP etc.

To Existing Extensions

Will existing extensions be affected?

To Opcache

It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.

Please explain how you have verified your RFC's compatibility with opcache.

Open Issues

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

Include these so readers know where you are heading and can discuss the proposed voting options.

Patches and Tests

Links to any external patches and tests go here.

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.


After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)


Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/to-array.1575481305.txt.gz · Last modified: 2019/12/04 17:41 by stevenwadejr