Newsgroups: php.internals,php.internals Path: news.php.net Xref: news.php.net php.internals:46564 php.internals:46565 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 44194 invoked from network); 29 Dec 2009 00:27:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Dec 2009 00:27:09 -0000 X-Host-Fingerprint: 75.72.251.154 c-75-72-251-154.hsd1.mn.comcast.net Received: from [75.72.251.154] ([75.72.251.154:4144] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 29/89-26502-DDC493B4 for ; Mon, 28 Dec 2009 19:27:09 -0500 To: internals@lists.php.net,Etienne Kneuss Message-ID: <4B394CDC.9040709@warpmail.net> Date: Mon, 28 Dec 2009 18:27:08 -0600 User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 CC: internals@lists.php.net References: <78.83.26502.F1DD83B4@pb1.pair.com> <4B38DDD3.8010108@hexon.cx> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 75.72.251.154 Subject: Re: [PHP-DEV] is_array on objects with ArrayAccess or Iterator implementations From: cpriest@warpmail.net (Clint Priest) Etienne Kneuss wrote: > On Tue, Dec 29, 2009 at 12:04 AM, Clint Priest wrote: >> Unfortunately $x instanceOf ArrayAccess doesn't return true when $x is >> indeed an array. > > Making is_array return true for objects implementing ArrayAccess is a > bad idea, for two main reasons: > > 1) is_array is a type check, and we should still be able to > distinguish real arrays from objects That's true of course, definitely would need to be able to distinguish. > 2) ArrayAccess does not guarantee that an object will behave like an > array, (e.g. you won't be able to use sort() on an object implementing > ArrayAccess. I wonder if this is something that users would be expecting, that any function which took an array would also take an object that implements certain interfaces (such as ArrayAccess and perhaps Countable). > If, in your case, you want to accept both arrays and ArrayAccess > objects, I guess if (is_array($v) || $v instanceof ArrayAccess) is a > sufficiently good way to check. > I'm finding myself implementing ArrayAccess more and more often because its so transparent to the consumer. The reason this came up is because I was considering returning all arrays from a certain section of my shared code library always be an array object so that things like $tblArray->pluck('Value') could be done, rather than array_pluck() type functionality. That got me to thinking about all of the is_array() calls I would need to replace throughout the codebase. Would it be terribly difficult to make objects with ArrayAccess completely interchangable anywhere an array element would be required by a function?