Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70579 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93490 invoked from network); 11 Dec 2013 01:23:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2013 01:23:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=chobieee@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=chobieee@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.173 as permitted sender) X-PHP-List-Original-Sender: chobieee@gmail.com X-Host-Fingerprint: 209.85.192.173 mail-pd0-f173.google.com Received: from [209.85.192.173] ([209.85.192.173:65335] helo=mail-pd0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DB/00-27591-69EB7A25 for ; Tue, 10 Dec 2013 20:23:35 -0500 Received: by mail-pd0-f173.google.com with SMTP id p10so8492350pdj.32 for ; Tue, 10 Dec 2013 17:23:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=lV9BL5DdtRL92FY90Sm+YO+iR6TOVOCaxcttDTXc4Fw=; b=Yvq/NdW7s/hGt8Gm4Fep9I0tYSfdpPycdr9H0u+g/T6+qlWWOj52dViJm6ujF0f4Rw Ow8iHmE6byLVd8Jp51ELFqpDpLAItx5ycKfo6EvRvHFnqRk/x5KYGBiAY2sLQz2PcGv0 y+GMruL8z32yfnkfkx3SGefb9uXHINltAqWj72V+0MwsWDtcp4NIcAuSSevcOmbV/Rp4 kGTqRz/prZiNnDrf3s/mES+ZFwV4VHvS8mgLz0nUTZ4kiiQso5Eqb45tMUbW1vx/OrsS BWitlheHyhgEXHX6pfJlmyOvE68pxmsXC94lKEdEfo/hCsQPGL2lS6Jb2vQfxQRzFrlV 0rSw== MIME-Version: 1.0 X-Received: by 10.68.196.227 with SMTP id ip3mr13835811pbc.163.1386725012150; Tue, 10 Dec 2013 17:23:32 -0800 (PST) Received: by 10.68.1.231 with HTTP; Tue, 10 Dec 2013 17:23:32 -0800 (PST) Date: Wed, 11 Dec 2013 10:23:32 +0900 Message-ID: To: PHP internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Proposal: ArraySerializable interface From: chobieee@gmail.com (chobie) 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