Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98539 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58540 invoked from network); 15 Mar 2017 17:49:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2017 17:49:56 -0000 Authentication-Results: pb1.pair.com smtp.mail=bburnichon@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bburnichon@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.53 as permitted sender) X-PHP-List-Original-Sender: bburnichon@gmail.com X-Host-Fingerprint: 74.125.82.53 mail-wm0-f53.google.com Received: from [74.125.82.53] ([74.125.82.53:37266] helo=mail-wm0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 66/DF-38004-2CE79C85 for ; Wed, 15 Mar 2017 12:49:55 -0500 Received: by mail-wm0-f53.google.com with SMTP id n11so29207473wma.0 for ; Wed, 15 Mar 2017 10:49:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=gwm+4FEyhN+bhj9b4HSpkeaeIa0D+fNNXTVKllhWE9U=; b=qMuQSTEJ98G+RsvTHtz5zsh/4BCSXXSKDp9gOlduABzQejvQnXc3QdSfDrW5lHUqLd eCZXnnfcujw3EH5AAU6t/9vrJYz8V+MB6YgFpOTDSfoBKq8HW5oKsmUKLJjsbaPAPe51 WOUaXQiSm9/ulhLSKIfqH6axfn7C+1rZ8hL9xIH11U238bH3zVL8ZGlt0oiK1JYxw2Jj nVeya4W03BAhSocq558EijYd3tg5rmdot1eUY7ZTFGa4jtLhri+DcXfc7QNwWM2f5N4C DaXooLKKafSyOhp3vPDaf1lpZPwuhdq851/L9kbcFzEcetSWB2VYrqSqluA9dCEm4fFr syAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=gwm+4FEyhN+bhj9b4HSpkeaeIa0D+fNNXTVKllhWE9U=; b=HSVri8S0nrn7ed/dRH4HCBYa7EvaqTD/TC0FLXDJMIDAk6cT58h1i6qK+dM7YFZvbG S4d5S3sbckWVFMK2z4EB0WgU6LceNoCzeSJOIPDKz/muZevLdv0RIQKBRtNonCUjDGr4 1ci5swLasc+EZO5V/n1OrIxUIG2XK/cbafpmtE6WyWSHYz/gza6hgGmbXlUe+xBDFCZC MKFzhOh7vrJer3IJ8K1pegxwsWJjvHf0cjt+McC7BoxxCb6DOT4jdsf5c7kF0rgEO4Ke yY3RpWySGM9/YbgxmYxta34UtAN0dR16yvSkqxeop42L0x7HWtHMnful6QhaihgGM4uB 9hnw== X-Gm-Message-State: AFeK/H1o+xgg0gD7chMssOsN7IjP//scKye0/6JJIxpwPEFYRf8y9hX018FxfAERSbfprIeMW2Y+ixAgtHCHGw== X-Received: by 10.28.86.214 with SMTP id k205mr5218133wmb.26.1489600190988; Wed, 15 Mar 2017 10:49:50 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 15 Mar 2017 17:49:40 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a11452cac3dcad2054ac89279 Subject: Add __toArray() method to objects that would be called on cast to array From: bburnichon@gmail.com (=?UTF-8?Q?Beno=C3=AEt_Burnichon?=) --001a11452cac3dcad2054ac89279 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi all, Looking at code of PHPUnit, I stumbled upon an inconsistent array conversion: ------ /** * @param ArrayAccess|array $other */ function evaluate($other) { // type cast $other as an array to allow //support in standard array functions. if ($other instanceof ArrayAccess) { $data =3D (array) $data; } $patched =3D array_replace_recursive($other, $this->subset); // ... } ----- This would work only for `ArrayAccess` implementations extending `ArrayObject` as shown by https://3v4l.org/ti4aY Looking at the manual http://php.net/manual/en/language.types.array.php#language.types.array.cast= ing , it seems `ArrayObject` class does not comply to array casting because integer public properties could also be retrieved. Some tests showed that regular class always have string keys even when a `$key =3D 0; $this->{$key= } =3D 'avalue';` is called. In this case, `var_export((array)$object);` retur= ns `array('0' =3D> 'avalue')` (Notice the quote around key 0 - https://3v4l.org/6QW70) What do you think of adding an optional `__toArray()` method to classes which would default to current behavior but would allow specifying behavior. The way of internal `__toString()` method and could explain inconsistency of the `ArrayObject` class? Regards, Beno=C3=AEt Burnichon --001a11452cac3dcad2054ac89279--