Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98545 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73406 invoked from network); 15 Mar 2017 20:41:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2017 20:41:22 -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.220.175 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 209.85.220.175 mail-qk0-f175.google.com Received: from [209.85.220.175] ([209.85.220.175:34707] helo=mail-qk0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B8/32-38004-0F6A9C85 for ; Wed, 15 Mar 2017 15:41:20 -0500 Received: by mail-qk0-f175.google.com with SMTP id p64so23583251qke.1 for ; Wed, 15 Mar 2017 13:41:20 -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=ZwO451esH7h14FYdaA0tuR9RlWaYklPUtLQapU8y2kg=; b=dU5lyb394sUqU3gWxspe7omc3twvKyW3j12c7q4vyT2Hdgwhs6/qfzUZ/bXba77uJ4 JXDW6NMbzz80qkxdlS0oBsbzRz9ghytBxlNY7GGmCdY22xrYM5T9Vgna863xgMim9BBu no1cDVNdgLYb7c6N1IBH3phVkf2lE8lhq3DLyqfRg+9Jo49ddJQgAMRtDsSbQe+sTq1/ V2R+8GaydVSU1LOsi3/iHeDacScKQJVyHvV4r/jQbox/Qt5j72uS0NHesp+YvUEEcefk NYZ5CU9T8di3dSIcjwNnDZlS3Ie1zLPLvOqWcsHOk9yp8k28CKEG3pmfK0Q4xX210iRg WEjQ== 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=ZwO451esH7h14FYdaA0tuR9RlWaYklPUtLQapU8y2kg=; b=fR5R7z12Z50XYYYyoQ5LiUmJY5C+DVEgw0lCTJX19iLI6eRl3Ot1uVlCkStb0i8G83 TUlv6NZK7TSAu9ibzyGx1ENmY57K+ZZ2T06GMAifWCmzdzAIm8LgHR6R6nQhYcpg11Qd 906nuiRcd2/jHzzS5f7vl9Z4taz6HMhxSwAo9OojoLRjtfmd3Z6tFfJTINAoK7bXzukv MgGFkfjXn+Gnk/EiEui0237XzaBnXzplyr97ieFBIgZS/uWOUc5WlUrPNlQr7FQZfqMP Nx2pWRc7hVdvvVWQGD5KesS+CJIj6UU7BScwpVv0uPEIr9pSLpaesDdwYEgetuZ0TB9Z gxYw== X-Gm-Message-State: AFeK/H2cob76Jb069xluWMkGtKzTgHWYcM9Ki6VPu+IxKEiQX3jTQfSQWvAZ9CnJFd9kQo5RWYfvIwF+ommU6w== X-Received: by 10.55.8.131 with SMTP id 125mr5076451qki.302.1489610477735; Wed, 15 Mar 2017 13:41:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.237.44.70 with HTTP; Wed, 15 Mar 2017 13:41:15 -0700 (PDT) Received: by 10.237.44.70 with HTTP; Wed, 15 Mar 2017 13:41:15 -0700 (PDT) In-Reply-To: References: Date: Wed, 15 Mar 2017 21:41:15 +0100 Message-ID: To: =?UTF-8?Q?Beno=C3=AEt_Burnichon?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary=001a114c568c61135d054acaf7b6 Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: ocramius@gmail.com (Marco Pivetta) --001a114c568c61135d054acaf7b6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable This is a BC break due to the fact that the `(array)` cast is used to extract property information from private properties in library code. On 15 Mar 2017 6:50 p.m., "Beno=C3=AEt Burnichon" wr= ote: > 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.casting > , > 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->{$k= ey} > =3D 'avalue';` is called. In this case, `var_export((array)$object);` ret= urns > `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 > --001a114c568c61135d054acaf7b6--