Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98540 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60786 invoked from network); 15 Mar 2017 18:14:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2017 18:14:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=derokorian@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=derokorian@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.182 as permitted sender) X-PHP-List-Original-Sender: derokorian@gmail.com X-Host-Fingerprint: 74.125.82.182 mail-ot0-f182.google.com Received: from [74.125.82.182] ([74.125.82.182:34825] helo=mail-ot0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 46/30-38004-A6489C85 for ; Wed, 15 Mar 2017 13:14:02 -0500 Received: by mail-ot0-f182.google.com with SMTP id x37so28295627ota.2 for ; Wed, 15 Mar 2017 11:14:02 -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=4W1g+BhOnzeKuurEQ+Y7K8zG+c20ewP6cSt2Plh133g=; b=XQI9wbPMtrYLgLHbs8VkZ+ui2HKxDimJST46yBWorwMZhpevztj2jBYClW6bw5GULG FQlZoSODQ11uFwsJuGdib7esHgB7HNU3mmBtWBqflbF3InCHHBG9VPf/BxBfqSOTiIIw vbF/vIZ2pts+ypwIrHfuS9YRUpiuBjUCBg0x9Q/r3tJwYk3xcWK5TOHebdKEfMWP5sxo LmWlzauJMb/NTap4PxGjrLGxlkD4/UVbV1V8Yws+tq94ugXi/v8g701rSxynTENmrCfv MIMyDRY7Ygs1E4i5vTrAm7cS2faGQWYmqmMqy8Rt1oC1JL1CodT+nIEjHEcu8NLUsbLD qEaQ== 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=4W1g+BhOnzeKuurEQ+Y7K8zG+c20ewP6cSt2Plh133g=; b=EreBP1Lev2ioutIxmb9Q9PrORrEiwawVCP20zTmiYpE/X2mtXPUox2O0tA6rhb7CoA byolon8mEmE3w9QMfBJ49o7dtyz+qDHp3JFFTjbthyAi1CfDdSEbCQuGDrUynDrULC9F R6Lqdo/voV/OAp+QGDqxHWYs5jsn3pGXOwU8DI5GM1dwN5hwfaH77MFOuM0tsjotxBbE qCnEgFXWpNUC7cza2U70I2arI3BCfV9Pq4IG3gb2IBR/wfvaFdXnQYfnwFhixVfeItR8 ghnQKJrWKhuyj+jWaEtXG8+VAYlnyhz3W8f21zjCKguw60OC9193Ock7euHpPec13QHV fkNA== X-Gm-Message-State: AFeK/H3lYGh2UWjBbNctppm3DzslpjDd4KivmH75Vkwcg2aE+x/nRJZH9GjtTZuo/YPjLeId1HdA9u2ztqh1Mw== X-Received: by 10.202.244.82 with SMTP id s79mr2274356oih.184.1489601638880; Wed, 15 Mar 2017 11:13:58 -0700 (PDT) MIME-Version: 1.0 Received: by 10.157.11.227 with HTTP; Wed, 15 Mar 2017 11:13:58 -0700 (PDT) In-Reply-To: References: Date: Wed, 15 Mar 2017 12:13:58 -0600 Message-ID: To: =?UTF-8?Q?Beno=C3=AEt_Burnichon?= Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a11c183bc8aff1a054ac8e80a Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: derokorian@gmail.com (Ryan Pallas) --001a11c183bc8aff1a054ac8e80a Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Mar 15, 2017 at 11:49 AM, Beno=C3=AEt Burnichon wrote: > 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? > I like the idea kind of, but would this remove the ability to cast to array all classes not implementing __toArray, as is the case with __toString? This would be a HUGE BC if so: $ php -r 'class Foo {public $foo =3D "foobar";} var_dump((array) (new Foo))= ;' array(1) { ["foo"]=3D> string(6) "foobar" } $ php -r 'class Foo {public $foo =3D "foobar";} var_dump((string) (new Foo)= );' PHP Recoverable fatal error: Object of class Foo could not be converted to string in Command line code on line 1 $ php -v PHP 7.1.2 (cli) (built: Feb 27 2017 00:02:44) ( ZTS ) > Regards, > > Beno=C3=AEt Burnichon > --001a11c183bc8aff1a054ac8e80a--