Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98567 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57655 invoked from network); 16 Mar 2017 14:02:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2017 14:02:24 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.181 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 209.85.220.181 mail-qk0-f181.google.com Received: from [209.85.220.181] ([209.85.220.181:35822] helo=mail-qk0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4C/1E-38004-BEA9AC85 for ; Thu, 16 Mar 2017 09:02:19 -0500 Received: by mail-qk0-f181.google.com with SMTP id v127so39657675qkb.2 for ; Thu, 16 Mar 2017 07:02:19 -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=rD7X0IBoLC4uoNVbW3yRKVIf6T7jdqduBP5zu1rKyxU=; b=Cr+lbelIykcQ1VY4J7CYZxpSWIMJ8SQE2Im0FOElh1bIqSuOmekFeQ1j3+alMoTUVG zLxH15vPLGGHmxgn4OhIi7BVZcm54W6FaQlYWCJc7jF1p6Ks9MTA3rRlk1njIVQxzRyb tahvRX8ztHKiMz3LvVgGYh9Rr/XuLzSIjsbf4mJgoZQDZvj0wGrRX09O+DC7xPxBoFIw x9KrpmFRORwpOC6x21ZLQoUJqt6xXT1CiYQ/xci6jT06P0SMvKa/sF5KDtCtrpVdala8 4ZUtgrK2VsbfboWg9QBM90EHEK4MCISQfTiVGIQ4T0j7CxNyNPHhqTbkyCVUmG4DAuu2 zu2g== 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=rD7X0IBoLC4uoNVbW3yRKVIf6T7jdqduBP5zu1rKyxU=; b=EZOhNiWJdOTGL2QKWfU2dBMj7BZ4+tmKbjtvb+lvTjKi8PjQb0L725aWOTZA1S+sme NJNB++M3VkdsN64gYygA4YZ7qKgKUWbibsa90315GvxMqsmkIseEBmlRL7ovICZ1np2v dEBhmBSeOrA7/zQsydfgAHeVWtXRJsyfwqOiy9po3QMR/OCTqyAjXmB2xW3vfbiiXhuY yONgY3ml1n/OlMhLTV1es9aIzhA9v43nMY1O53nXVQXrWRyHf7eRu8fChfeWFfb1FiHU NTwcu2/kGW5iIoVTGr2FyB/hPa5khgDdFmjYH7kkvdjSr+MHdwwQukDiTd8VZqqoBPrF Zk7w== X-Gm-Message-State: AFeK/H2s3tIdWXyrtKP9VWYmaxj20NvEO16Pm32stoQbO9g1/5MY69URttg/0SSW8oktayRlfNxnjyP6rB1hWQ== X-Received: by 10.55.198.92 with SMTP id b89mr8958756qkj.63.1489672936241; Thu, 16 Mar 2017 07:02:16 -0700 (PDT) MIME-Version: 1.0 Received: by 10.237.44.70 with HTTP; Thu, 16 Mar 2017 07:01:55 -0700 (PDT) In-Reply-To: References: Date: Thu, 16 Mar 2017 15:01:55 +0100 Message-ID: To: Andrey Andreev Cc: Ryan Pallas , PHP Internals List , =?UTF-8?Q?Beno=C3=AEt_Burnichon?= , Kalle Sommer Nielsen Content-Type: multipart/alternative; boundary=001a114787a6325df5054ad9825f Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: ocramius@gmail.com (Marco Pivetta) --001a114787a6325df5054ad9825f Content-Type: text/plain; charset=UTF-8 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: 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/ --001a114787a6325df5054ad9825f--