FAQ
Hi,

I've got an idea for adding common way to convert array from object:
ArraySerializable interface and to allow the changing of existing
array conversion mechanism.
As an example of this change, consider the following code-snippet:

     $person = new StdClass();
     $person->name = "John";
     $phone = new StdClass();
     $phone->number = "12345";
     $person->phone = $phone;

     var_dump((array)$person);
     #array(2) {
     # ["name"]=>
     # string(4) "John"
     # ["phone"]=>
     # object(stdClass)#2 (1) {
     # ["number"]=>
     # string(5) "12345"
     # }
     #}

Currently, the implicit object to array conversion does not work
recursively. This propose changes object to array conversion behaviour
which implements ArraySerializable interface. specifically
ArraySerializable::__toArray method overrides current (array) cast.

     interface ArraySerializable
     {
         /** @return array */
         public function __toArray();
     }


ArraySerializable interface provides common way to convert to array
from object. also impose conversion rule.

* __toArray() returning value excepts an array and It values only
accepts primitive type. (long, double, string and array)
    * do cast to array operation when the value contains object which
implements ArraySerializable interface
   * otherwise, raise RuntimeException.
* __toArray() method calls implicitly when cast to array from object.

This feature improves object to portable format (like json) conversion
mechanism.

rough propose document and patch are here:
https://gist.github.com/chobie/7890899

I want to get feedback about this propose. If I get a good response
I'll investigate potential issues and improve rfc and patch.

Thanks,
Shuhei

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 10 | next ›
Discussion Overview
groupphp-internals @
categoriesphp
postedDec 11, '13 at 1:23a
activeDec 16, '13 at 7:19a
posts10
users4
websitephp.net

People

Translate

site design / logo © 2019 Grokbase