Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98544 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69188 invoked from network); 15 Mar 2017 19:36:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2017 19:36:48 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.86 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.86 mx105.easyname.com Received: from [77.244.243.86] ([77.244.243.86:50880] helo=mx105.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1E/A1-38004-DC799C85 for ; Wed, 15 Mar 2017 14:36:46 -0500 Received: from cable-81-173-135-7.netcologne.de ([81.173.135.7] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1coEiu-0001Zz-Qb; Wed, 15 Mar 2017 19:36:42 +0000 Reply-To: internals@lists.php.net References: To: =?UTF-8?Q?Beno=c3=aet_Burnichon?= , internals@lists.php.net Message-ID: <3dbc7b3a-d320-4320-cdf1-61bf28b8dc0f@fleshgrinder.com> Date: Wed, 15 Mar 2017 20:36:35 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-DNSBL-PBLSPAMHAUS: YES Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: php@fleshgrinder.com (Fleshgrinder) On 3/15/2017 6:49 PM, Benoît 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 = (array) $data; > } > > $patched = 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 = 0; $this->{$key} > = 'avalue';` is called. In this case, `var_export((array)$object);` returns > `array('0' => '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ît Burnichon > Wouldn't it also be possible to add a `Arrayable` interface with a totally normal `toArray` method? This would avoid littering code bases even more with feature detection code like `method_exists($x, '__toString')`. There would be nothing special to do here, other than implementing that interface for all core classes for which it makes sense. The only question that remains (regardless of the strategy) would be: is it iterable? I guess the answer "yes" is obvious here. Hence, `Arrayable` would need to extend at least `Traversable`. -- Richard "Fleshgrinder" Fussenegger