Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98568 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60460 invoked from network); 16 Mar 2017 14:34:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2017 14:34:49 -0000 Authentication-Results: pb1.pair.com smtp.mail=marcio.web2@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=marcio.web2@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.182 as permitted sender) X-PHP-List-Original-Sender: marcio.web2@gmail.com X-Host-Fingerprint: 209.85.161.182 mail-yw0-f182.google.com Received: from [209.85.161.182] ([209.85.161.182:32967] helo=mail-yw0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/7E-38004-882AAC85 for ; Thu, 16 Mar 2017 09:34:48 -0500 Received: by mail-yw0-f182.google.com with SMTP id v76so33160369ywg.0 for ; Thu, 16 Mar 2017 07:34:48 -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=oueK2LvhRlHW3HaIpe4Lt4H6vTCv04yH9+8ofz4OPR0=; b=H6B/GZrM65BE3mrASBn1Db0WmQBtjt552k7CRdvvVJmJBifzQq1jWDDg9xEHd01JFc T8zZxZ1XtaSfukCRWW9HNzAF4DPCMsv5AVxpc9E1IzvpmpPGJ5UGdAbXdCzF6iFPlI6T +W6mn3HnlZ3SwaIH/lVSncLVdx85aM1YS7XXXdQn31PhY/6dVdvRuLtQXfVO1dZ6i8WG 38ethaQ7aDk/RzQD8vV2dDvHuRDBVJ62Zd3+RYU32dUhKA44vMOSJ8r7eMit0w2M63tg uDNW3LFCAUL48VBQe50SqocwqjECnnPwYKwBctlKy4+z9LzTsP4wKPnany+z3SSMUGoe aaog== 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=oueK2LvhRlHW3HaIpe4Lt4H6vTCv04yH9+8ofz4OPR0=; b=UFMeLQAo1+MK1mX3wF5CjtI/pLZH8cneB0x04Pkq+7s5HSHsrmUDgYWO1r67rZVvoy MYYd2JkabW/hpIrBEx4qlYYZhHAoFubUIDzvv1sHUxE7YyRzqBuIXc0ypTiRBQ7Y03qg iCYYI4J+P7Ue7Zox7udSDZ/GKpOBgFTzqT2WhHhj33EmBZ0ERxy9MQF90vitRbGeUcux pilwG6EB1EhJQDEa+S7PUIoG2/XxCLk2snITGG91FJaXc4Yp3PF4B1a1iHGhHnuMtkFm z6bP8MaYueR3hAlDrW5pNW09WiGnh4bN503880xfiaYht1yT0nlBfIeXQbcg0L8Jt7KN /JVA== X-Gm-Message-State: AFeK/H29bmujHil1JQ+kzZ06saDE1aSk5qtUOBqZXMyIBROQ0x1A+99UGDMW0hlGD+AP7q454s0i+wI6h1U40w== X-Received: by 10.129.130.1 with SMTP id s1mr7083392ywf.132.1489674885587; Thu, 16 Mar 2017 07:34:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.37.176.166 with HTTP; Thu, 16 Mar 2017 07:34:25 -0700 (PDT) In-Reply-To: References: Date: Thu, 16 Mar 2017 11:34:25 -0300 Message-ID: To: Marco Pivetta Cc: Andrey Andreev , Ryan Pallas , PHP Internals List , =?UTF-8?Q?Beno=C3=AEt_Burnichon?= , Kalle Sommer Nielsen Content-Type: multipart/alternative; boundary=94eb2c07b87462eb81054ad9f6b8 Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: marcio.web2@gmail.com (Marcio Almada) --94eb2c07b87462eb81054ad9f6b8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 =3D 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 unreliabl= e > 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)` ca= st > 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. Thanks, M=C3=A1rcio. --94eb2c07b87462eb81054ad9f6b8--