Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108420 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18302 invoked from network); 7 Feb 2020 00:20:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Feb 2020 00:20:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D666C1804F6 for ; Thu, 6 Feb 2020 14:33:38 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8943 85.119.80.0/21 X-Spam-Virus: No X-Envelope-From: Received: from mail.apserver.co.uk (server2.alteredperspective.co.uk [85.119.82.103]) by php-smtp4.php.net (Postfix) with ESMTP for ; Thu, 6 Feb 2020 14:33:37 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.apserver.co.uk (Postfix) with ESMTP id DD3E24F4064; Thu, 6 Feb 2020 22:32:59 +0000 (GMT) Received: from mail.apserver.co.uk ([127.0.0.1]) by localhost (server2.alteredperspective.co.uk [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 08uDPHZNA5un; Thu, 6 Feb 2020 22:32:57 +0000 (GMT) Received: from [192.168.0.7] (unknown [86.18.119.17]) by mail.apserver.co.uk (Postfix) with ESMTPA id 327694F4061; Thu, 6 Feb 2020 22:32:57 +0000 (GMT) To: Steven Wade , PHP Internals References: Message-ID: <8e29b802-44e7-08be-eddf-18cabd159bf8@allenjb.me.uk> Date: Thu, 6 Feb 2020 22:33:34 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC - discussion] __toArray() From: php.lists@allenjb.me.uk (AllenJB) What happens if I perform array-like operations on an object implementing __toArray() from this RFC? For example: $obj["foo"] = 42; or: $bar = $obj["foo"]; (Imagine these examples are perhaps within a loop iterating a collection of objects) Which operations do and do not work? As they'd be operating on a new from-cast array, I assume they would not be able to effect the original object (properties), but is this what people expect? What I'm trying to get at here is: Does this RFC create opportunities for bugs arising because objects are accidentally treated as arrays and users would no longer receive any kind of warning or error from such code when they would have in the past? (Aside: Could this get even more interesting where a loop involving references being (ab)used is involved? Experienced developers know you should avoid references, but many newer developers use them - either through misunderstanding the language or copying others and not actually understanding what they're doing. While, anecdotally from helping others in various channels, this occurs less than it did in the PHP 5 era, it does still occur.) If so, I believe this is obviously bad. If not, I believe this is also bad because the above example do not work as someone (particularly newer users) might expect. The language is creating something which sometimes, maybe, acts like an array, but not always. The other problem I have with this type of magic on objects is that people don't usually mean "to array", they mean "to array for specific purpose" - eg. "to array for database record" or "to array for API output". Using a magic method for this obfuscates the purpose of the returned array and could lead to problems, such as accidental data leakage, from cross-usage. AllenJB On 04/02/2020 13:03, Steven Wade wrote: > Hi all, > > I’d like to officially open my __toArray() RFC up to discussion. I’ve delayed changing the status until I had more time to respond to the discussion, but since it’s been brought up again , I figured now is the best time. > > https://wiki.php.net/rfc/to-array > > Cheers, > > Steven Wade