Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98569 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62504 invoked from network); 16 Mar 2017 14:56:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2017 14:56:00 -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.216.181 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 209.85.216.181 mail-qt0-f181.google.com Received: from [209.85.216.181] ([209.85.216.181:36334] helo=mail-qt0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D6/DE-38004-F77AAC85 for ; Thu, 16 Mar 2017 09:56:00 -0500 Received: by mail-qt0-f181.google.com with SMTP id r45so39600872qte.3 for ; Thu, 16 Mar 2017 07:55:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=QefXM2F3kCBP5vuJTsQtKVQ9hc06wOdsfE4LO3eW6S4=; b=X0GEHwREb7QXQP3kJeDoZi2vSSmjf6DwVXR5DGYu9FNIlFqbzISz0OuX0/7LD06LZi l3DJmvBBZndbFo5Z6B7e4kVpHeuKQX/l6M8lAqggt6FLcCqE1nBB58DXOwLsX75YYV5N /yaEzD6dWog0jvjRxgIkbo17nVZEM/zA2Ft3LrZrDuDTevUfZII+jnbEj3U/FOOWf5yh oXGhBsIgT5G7gq1uM8Ifnh1lKbG15DeX/F0RmXwV3tCiNmzn99zJ7bXIBvf9O2ZiV2el +IYKzBq7DM1/M5sF7WPHox9mejycGc46ufPd68KA5wRewY9IBHtx/63jYxFJevJIekBN ykTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=QefXM2F3kCBP5vuJTsQtKVQ9hc06wOdsfE4LO3eW6S4=; b=GVMmFq+8qM7riy7Q4imWayCjlcxpfLEnBuQQp76Kne968uPcYypQh/EDrmiAHjhm0o 8Qb9HV6rsb/TgCht20lvKp5iiiq/OTVDyWyGHghAzzur40CzcrA3tcbvwQTDQJrFmvyI lwf/v1q+IDNTLgKcOasSKdtbHlGhxj8nUEbigfe54dmB7nRYT1729I8q3XpMkelIBJS4 KHDUKX3tuLagEgVJvIfPX8Oq7AfNQHNhbutkSPvU9jZu5lL4XfXQKGaXWihngEZXa5Ac imtWPHmzdg9TFiEC+Th5Eo9yGKj4v7pBqXWhKpYx6rMWUc+CPMctFKtPcCrlodgd825r Rqqw== X-Gm-Message-State: AFeK/H38l51Q9wQfJ5YWdOS3wTTRq29o3EOjlRC5MxQrbd/eAjyo9vCNTC6uimIRPQPqDTHz4oQ5PQ4GFu/NvQ== X-Received: by 10.237.41.100 with SMTP id s91mr9345624qtd.143.1489676156983; Thu, 16 Mar 2017 07:55:56 -0700 (PDT) MIME-Version: 1.0 Received: by 10.237.44.70 with HTTP; Thu, 16 Mar 2017 07:55:36 -0700 (PDT) In-Reply-To: References: Date: Thu, 16 Mar 2017 15:55:36 +0100 Message-ID: To: Marcio Almada Cc: Andrey Andreev , Ryan Pallas , PHP Internals List , =?UTF-8?Q?Beno=C3=AEt_Burnichon?= , Kalle Sommer Nielsen Content-Type: multipart/alternative; boundary=94eb2c0856d42b1d72054ada4297 Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: ocramius@gmail.com (Marco Pivetta) --94eb2c0856d42b1d72054ada4297 Content-Type: text/plain; charset=UTF-8 On Thu, Mar 16, 2017 at 3:34 PM, Marcio Almada wrote: > 2017-03-16 11:01 GMT-03:00 Marco Pivetta : > >> Since some folks keep banging on "it's not a BC break", I propose a >> challenge in fixing this particular BC break example (reads: find a >> different way to make it work, and no warnings/notices allowed): >> >> I made a very simplistic example of where `__toArray` will break existing >> API that currently literally accepts any kind of object: >> https://3v4l.org/Tj0GH >> >> For reference, here's the code copy: >> >> > >> class Foo >> { >> public $bar; >> public $baz; >> public $taz; >> } >> >> // please note that this API has a signature of `object $object` - any >> object allowed. >> function unsetProperties($object) { >> // we ignore inheritance and private properties for simplicity >> return array_diff( >> array_map( >> function (\ReflectionProperty $p) : string { >> return $p->getName(); >> }, >> (new ReflectionClass($object))->getProperties() >> ), >> array_keys((array) $object) >> ); >> } >> >> var_dump(unsetProperties(new Foo)); >> >> $foo = new Foo; >> >> unset($foo->baz); >> >> var_dump(unsetProperties($foo)); >> >> There are dozens of simpler examples that break (typically, data-mapper >> layers): I picked this particular one because there is no other way to >> detect unset properties without causing side-effects. >> >> If `__toArray` is implemented on an object, the API defined above will >> either report all properties to be unset, or in general produce unreliable >> results. >> >> The current API specification of the `(array)` operator is as following: >> >> * for every SET property produce an array value, with the key being: >> * "\0" . $className . "\0" . $propertyName for private properties >> * "\0*\0" . $propertyName for protected properties >> * $propertyName for public properties >> * $propertyName for dynamically defined properties >> >> This is a **VERY SPECIFIC** behavior that the operator currently >> guarantees. Break that, and you can go on github hunting for `(array)` >> cast >> usages. >> >> The behavior was frozen in the current suite in >> https://github.com/php/php- >> src/blob/8015daeddb7bb4951c808fa253b97753787fb0ea/tests/classes/ >> >> array_conversion_keys.phpt (although, now that I notice, doesn't cover >> dynamically defined properties - should add that). >> >> Marco Pivetta >> >> http://twitter.com/Ocramius >> >> http://ocramius.github.com/ >> > > I kinda agree that (array) being aware of toArray() is not a very good > idea at this point, but... > > It's still not a BC break. In order for the output of "(array) $obj" to > change, one would need to change the implementation of the subject $obj > class. Lawyering this as a BC break will just hurt any possibility of > advancing the conversation in any direction. > > IMMO, if anyone would like to proceed with this toArray() proposal, a > better object reflection/destructuring API could be provided first. > That's pretty much the point: we need to have a way to keep API that works with `object` to keep working with `object` (BC). A different API for extracting all properties is sufficient, as long as it stays stable and survives the `__toArray` change. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --94eb2c0856d42b1d72054ada4297--