Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70582 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 604 invoked from network); 11 Dec 2013 02:37:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2013 02:37:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=mtdowling@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mtdowling@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.43 as permitted sender) X-PHP-List-Original-Sender: mtdowling@gmail.com X-Host-Fingerprint: 209.85.160.43 mail-pb0-f43.google.com Received: from [209.85.160.43] ([209.85.160.43:48208] helo=mail-pb0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C0/71-27591-DDFC7A25 for ; Tue, 10 Dec 2013 21:37:18 -0500 Received: by mail-pb0-f43.google.com with SMTP id rq2so8983850pbb.2 for ; Tue, 10 Dec 2013 18:37:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:mime-version:in-reply-to:content-type :content-transfer-encoding:message-id:cc:from:subject:date:to; bh=6bImYerwwa4Y+nktHC5rNqyAxYeBKnGR1lubYQ7mJv0=; b=JKX4Xv6SHtZHiE2kB8vyo771mSza//uohmnA9D4DozCqmBeElCXptpFzZY6FEMMdOs RuzYudYjdJeAy7KFePfGRAp4E10nUWwy+FdHPE2/9o3dZ00fUgT6lDjeLSeKDEveVTWP 1ckCv+TMTW3XUvveNF2CRMX22sBIxMVHN/ygunUBbJoi0GuXGWg8IfMu9+XssJmjXcn5 RwTy1Mir5s1XIb9mp/wM5lSRgLPfrZcZ2GIzNgJtEDzediUU8nJb6ZuUPgF5xsLk7khy YPwvlQkeNlQSC3MJwKzh0dEgt4bnfrFmtl120NNL7AcoUEKqdQdV+kY64TcmrSDus1hC 2Apw== X-Received: by 10.68.91.3 with SMTP id ca3mr31016063pbb.20.1386729434207; Tue, 10 Dec 2013 18:37:14 -0800 (PST) Received: from [10.150.12.106] (mobile-166-147-080-049.mycingular.net. [166.147.80.49]) by mx.google.com with ESMTPSA id lh13sm39984263pab.4.2013.12.10.18.37.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 10 Dec 2013 18:37:12 -0800 (PST) References: Mime-Version: 1.0 (1.0) In-Reply-To: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Message-ID: Cc: PHP internals X-Mailer: iPhone Mail (11A465) Date: Tue, 10 Dec 2013 18:37:12 -0800 To: chobie Subject: Re: [PHP-DEV] Proposal: ArraySerializable interface From: mtdowling@gmail.com (Michael Dowling) I think this would be a great feature. PHP already casts objects to arrays, s= o being able to customize that behavior would be really helpful. For example, Guzzle uses a similar interface to handle marshaling objects to= associative arrays when serializing requests. -Michael > On Dec 10, 2013, at 5:23 PM, chobie wrote: >=20 > Hi, >=20 > 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: >=20 > $person =3D new StdClass(); > $person->name =3D "John"; > $phone =3D new StdClass(); > $phone->number =3D "12345"; > $person->phone =3D $phone; >=20 > var_dump((array)$person); > #array(2) { > # ["name"]=3D> > # string(4) "John" > # ["phone"]=3D> > # object(stdClass)#2 (1) { > # ["number"]=3D> > # string(5) "12345" > # } > #} >=20 > 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. >=20 > interface ArraySerializable > { > /** @return array */ > public function __toArray(); > } >=20 >=20 > ArraySerializable interface provides common way to convert to array > from object. also impose conversion rule. >=20 > * __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. >=20 > This feature improves object to portable format (like json) conversion > mechanism. >=20 > rough propose document and patch are here: > https://gist.github.com/chobie/7890899 >=20 > I want to get feedback about this propose. If I get a good response > I'll investigate potential issues and improve rfc and patch. >=20 > Thanks, > Shuhei >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >=20