Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70580 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96242 invoked from network); 11 Dec 2013 01:37:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2013 01:37:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.42 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 209.85.128.42 mail-qe0-f42.google.com Received: from [209.85.128.42] ([209.85.128.42:55805] helo=mail-qe0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B0/B0-27591-DC1C7A25 for ; Tue, 10 Dec 2013 20:37:18 -0500 Received: by mail-qe0-f42.google.com with SMTP id b4so4954827qen.1 for ; Tue, 10 Dec 2013 17:37:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=m2Oc3p13Yto8SdOISx59NDZrXZV1GUwGfSiUlhNotnY=; b=IB0NdqpCElW2HFQjrj3hJ4TUFTVhUlk8Up/ZayuG+/OyjINdWdOuqv9rida80Yn0Rl wnKtasPErFMGGLxBhVzeOE6hexl3klVHn6F53UEyxD44n+xgzSLqA/OBylpleqd3+KF+ g8txWKdHFRr8dzuBeBBS6Cyrt+7XIOX+/Nq3v7Z7YHA0iKIBx+Te63/HXrO2ha50XzvQ SFguG9BS0rSVDYiBwRunsK048id+xUHdkPxAdHF0oKf7SHxNrQH2xR9MIT0CgipKiQGz +B4Q49VTH3lMr7n7hPfka11B62P4Yi3Y54ecgpOYsrFo6gldlBu4Alp8t/f25SnV9Pp7 DZWQ== X-Received: by 10.229.65.130 with SMTP id j2mr48769285qci.6.1386725835052; Tue, 10 Dec 2013 17:37:15 -0800 (PST) MIME-Version: 1.0 Received: by 10.140.23.176 with HTTP; Tue, 10 Dec 2013 17:36:55 -0800 (PST) In-Reply-To: References: Date: Wed, 11 Dec 2013 02:36:55 +0100 Message-ID: To: chobie Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113397cecc9caf04ed3844b5 Subject: Re: [PHP-DEV] Proposal: ArraySerializable interface From: ocramius@gmail.com (Marco Pivetta) --001a113397cecc9caf04ed3844b5 Content-Type: text/plain; charset=UTF-8 I am personally (currently) relying on the approach where array conversion gives me the values of the various properties as objects (when they are objects), so I wouldn't like another magic operator in there (to deal with/workaround). Wondering why this approach would be better than doing a recursive array map iteration. Pardon my naive and inelegant way of dealing with maps (see the running example at http://3v4l.org/ZOodL ): class ObjectToArrayConverter { /** * @param mixed $value * * @raturn array */ public static function toArray($value) { if (! (is_object($value) || is_array($value))) { return $value; } if (is_object($value) && method_exists($value, '__toArray')) { return $value->__toArray(); } return array_map(__METHOD__, (array) $value); } } Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On 11 December 2013 02:23, chobie wrote: > 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a113397cecc9caf04ed3844b5--