Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108390 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27493 invoked from network); 4 Feb 2020 16:49:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 16:49:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 10A8E180537 for ; Tue, 4 Feb 2020 07:01:28 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f176.google.com (mail-vk1-f176.google.com [209.85.221.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 4 Feb 2020 07:01:24 -0800 (PST) Received: by mail-vk1-f176.google.com with SMTP id o200so5242787vke.4 for ; Tue, 04 Feb 2020 07:01:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iDBNPPV+ZLbR5i9321jYBEgZPBurftwFAqJ6g9sns/M=; b=rChaq3tUwzOnODu748dEQ/DzZgC6kA+IoPYCBPq0460AXyt7lCBN+75N13gSWXZcM1 vg63KzPOapqeZZNYMG73NQk2SQqk4rluWJEdoNeVO7NiPn5b0WxUEWWgg2XNqSkxzZnc +cy2bcQJ6L1wUHq5tar8ee1rbZj2ZiINLhkc3FWDSc48GiDN2SaqnYgRmoJlRS4RJ5mi NWnqPcNy2NDQ5T+FPU+aqFU40MAk8tAhnYN+Gifg88OC99KGsRdCMncTxFu4UaKH0KKw l+yYESaF4D6o1iGWMAfEUqNPTOueYndo382qf9LeAEmFPPYL0eXTOWJIA5yeUaM2M4IC bg2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iDBNPPV+ZLbR5i9321jYBEgZPBurftwFAqJ6g9sns/M=; b=sUMaC9lO6yddhp7Mu3YA3TmzT0qZFnZCl171E3yng9PzQmGt7yOOMPuBe62dcgop7M H/un0PrRFP/Vn9vbQnXn0wu8wNYe1g+fI9pQcigwyngNJOp65Fo9wi8714wEddcYMIVo vru4N6/NB58A8QkFmbRJT1YEX68MfXvtvM0QfuLhaaHzIJoCyTFxm9n8yo5NRC7J2747 g/RB2XgaFqFBwqbp+hC6QDwBr1G21Ju4KCzxL8KWgv0JiySui2RTSIhk2rYiKKu4O7ZV fRpHVkhhc4baDs4EQwoSYE85nuVxD/4Q3T7wy4I6+Sqf6ZEE723qawS6a8m6OzpJyYo0 aIvA== X-Gm-Message-State: APjAAAXvtODzQBMfpHd4+SzDZ5LyY5VzNXlTcQe0wEsM0uyQFHBNZZwr SiowOyrGQsH/8MliypVLUHGps8dK2Mv+RTm2i5PRsA== X-Google-Smtp-Source: APXvYqwxxv42zPXfGN4ExBGnotopjNLzQKC5MVsx+C0BlEuQNcqsrMMPudfMX4fw24kNuHCPkzgXeSAsZb+hrKkK3bc= X-Received: by 2002:a05:6122:40b:: with SMTP id e11mr17544210vkd.21.1580828482852; Tue, 04 Feb 2020 07:01:22 -0800 (PST) MIME-Version: 1.0 References: <5077f0f4-4532-5131-0c8b-8b38017d66b5@aimeos.com> In-Reply-To: Date: Tue, 4 Feb 2020 10:01:11 -0500 Message-ID: To: Marco Pivetta Cc: "Aimeos | Norbert Sendetzky" , Benjamin Eberlei , Steven Wade , PHP Internals Content-Type: multipart/alternative; boundary="0000000000002bdcb4059dc1500a" Subject: Re: [PHP-DEV] [RFC - discussion] __toArray() From: chasepeeler@gmail.com (Chase Peeler) --0000000000002bdcb4059dc1500a Content-Type: text/plain; charset="UTF-8" On Tue, Feb 4, 2020 at 9:23 AM Marco Pivetta wrote: > On Tue, Feb 4, 2020, 14:50 Aimeos | Norbert Sendetzky > wrote: > > > Am 04.02.20 um 14:43 schrieb Marco Pivetta: > > >> I think we can't classify it as BC break, because no existing code > > >> implements __toArray at the moment, and hence it will not fail when > this > > >> feature is introduced and code gets upgraded to newer versions. > > > > > > It is a BC break because it changes the semantic of `(array) $object`: > > the > > > operation is no longer stable between two versions of the language. > > > > It wouldn't be a BC breaking change if `(array) $object` works like > > before when __toArray() isn't implemented by an object. As nobody should > > have implemented __toArray() because it's a reserved name for magic > > methods, we should be fine. > > > > The operation in question, when seen by its signature, is: > > (array) :: object FieldTypes -> Map String FieldTypes > > The proposed RFC changes this to (pardon the weird union type: my type-fu > is not that advanced): > > (array) :: (FieldTypes|IO ToArrayTypes a) => object a -> Map String a > > This changes the return type of a very much pure function (even makes it > non-pure: fun), and is a very, very, very clear BC break. > > > > I think we all know that I'm very big on avoiding BC breaks. I personally don't see this as a BC break, though. At least not one with PHP. Right now, behavior when casting things to an array is like so: (array)$scalar ==> [$scalar] (array)$array ==> $array (array)$object ==> [$prop1=>$val1, $prop2=>$val2, ...] So, assuming that right now I have the code: $x = new SomeThirdPartyArrayLikeObject(); //stuff $y = (array)$x; //$y ==> [$prop1=>$val1, $prop2 => $val2, ...] In a future version, in order to make that library more array-like, the following is added: public function __toArray(){ return [$this]; } Based on that, I'd argue that the BC break is with the library, not PHP. If the __toArray function is not implemented on that class, then nothing changes. The only way you'd get BC breaks with PHP itself is if core (and, arguably extension) classes started behaving differently when cast to an array. I'm personally in favor of anything that is going to allow us to create array-like objects that can be treated like arrays. I personally hate having to write: if(is_object($var)){ $x = [$var]; } else { $x = (array)$var; } No, the other question is whether we do it with a magic method, like __toArray() or an interface. I personally like magic methods, but, in the end I'm ambivalent on that. -- Chase Peeler chasepeeler@gmail.com --0000000000002bdcb4059dc1500a--