Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46566 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50299 invoked from network); 29 Dec 2009 02:01:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Dec 2009 02:01:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 72.14.220.157 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 72.14.220.157 fg-out-1718.google.com Received: from [72.14.220.157] ([72.14.220.157:30969] helo=fg-out-1718.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 21/7A-26502-6F2693B4 for ; Mon, 28 Dec 2009 21:01:26 -0500 Received: by fg-out-1718.google.com with SMTP id 19so3614020fgg.11 for ; Mon, 28 Dec 2009 18:01:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=2pUghGa7Qx7jx0+6KR3jelu9ynr6KXWNL0xO2uKyQsg=; b=CbJsTMoHsH1zPm6wPIdy4j0y7BLpIQlfVXpwfhRzYUpqhX7tgavYjlt0GoXU4W3Lp5 aLt80OksoJskvysI+rTps/GC1j8s+26UNP2WCPF4VN7hyfHJEBb2hUTAHxICBYP3NUp7 iZA9VVQYO5ryQ+/NWxipKT7A41PnY1o0qv+p4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=yBHfUkbWFCVqRFACz8QhiS5J8L2hM+Qn0fuEEBSHO0InEXgBoWbaRY8Gfj3wYB/uZO ftF8QFllY06MaAvmo+YKBQ4eSYf0TkQh5phKB5LCiGR8msHvQNi1d3/gsuckQk6AGiAz L+dg4IPS93KKfqlKCXnFjjzb/XZRyanIufy2o= MIME-Version: 1.0 Sender: ekneuss@gmail.com Received: by 10.87.42.6 with SMTP id u6mr8682138fgj.9.1262052083578; Mon, 28 Dec 2009 18:01:23 -0800 (PST) In-Reply-To: <4B394CDC.9040709@warpmail.net> References: <78.83.26502.F1DD83B4@pb1.pair.com> <4B38DDD3.8010108@hexon.cx> <4B394CDC.9040709@warpmail.net> Date: Tue, 29 Dec 2009 03:01:23 +0100 X-Google-Sender-Auth: 2f78dc26332e2e3b Message-ID: To: Clint Priest Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] is_array on objects with ArrayAccess or Iterator implementations From: colder@php.net (Etienne Kneuss) Hi, On Tue, Dec 29, 2009 at 1:27 AM, Clint Priest wrote: > 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) =A0is a >> sufficiently good way to check. >> > > I'm finding myself implementing ArrayAccess more and more often because i= ts > so transparent to the consumer. =A0The reason this came up is because I w= as > 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 completel= y > interchangable anywhere an array element would be required by a function? Yes, definitely, it would be a quite big amount of work (basically rewriting every functions dealing with arrays), and the interface that ArrayAccess proposes isn't enough for most array tasks. > --=20 Etienne Kneuss http://www.colder.ch