Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103733 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57595 invoked from network); 14 Jan 2019 18:25:04 -0000 Received: from unknown (HELO mail-io1-f52.google.com) (209.85.166.52) by pb1.pair.com with SMTP; 14 Jan 2019 18:25:04 -0000 Received: by mail-io1-f52.google.com with SMTP id r200so17795627iod.11 for ; Mon, 14 Jan 2019 07:00:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=CrTjw5DbynGck8NleqJvc4vaQtiIGnRr+Eo49S4aqWQ=; b=do827HpWgwAydwrCbAaGRLNCqHDvv+J7V6B12vj0isH/XRshSU8Dpc1HuHvT/DNj1W TyOxB1xgJG9CavMqZiQH5JUFEdZwQLjpxcAPtT3oSQG1vWpNRP2GT6bRwTlisLKC/QZ0 N04WOX+yzaJ+3Un5C+qrrzJ4cbD2z3iL5TQFKU6ih0ppU6mtQU6TfVk8+JFAjjUEecZk t3im+EqId/FdBkybj5uzcuOCaKZyoABGsf42dhzqmcLz+SWGyLGQnwcPsGga62I+LXtG O0+/eoHEJvhc57pIUCytePwOLW/CAfhcTEErCiiacI2ASVUXXqJ/itXWg2F2wuxMP6sV dixg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=CrTjw5DbynGck8NleqJvc4vaQtiIGnRr+Eo49S4aqWQ=; b=BySDW8DPwgfzgp+PfnnfHJQ/YCt4dWOh3waw0o2RSqQuj2mxpke2TTYM9yfv5Shms+ AIu2DGIHFNpNIea8Cq53vnXo7jLYYUNxZ7FViVKg1fiUqkk30+SjpwEOww6X1domox81 G0lbHCr1xG+xrv5yc9DKXPbLnyRIX3fctKQnbr42xbY9YBOmv+FR3CcO3lB5+cZiKTdg /5mZ/FE4zo6BXqu6wP/FjJQBU0AYDH+6DQkK3pXaAWEmHVWOT0jqphx/zwAFVnGgrlfu 9jIxSj0dmyP/nx5i0lZVa7F2/zC4IiL3N6/NsdOU+Hv0KGBpmpgphAgBLAwm4xRms8Yn zOcw== X-Gm-Message-State: AJcUukfjA6I2+TXhe+MXGThCVjX4D1vOt+mLXDnwGImVP/k2+FI8Plv0 /SECm6TH+A5qZ/mggrZclZVo128kTlxbks72Mu4= X-Google-Smtp-Source: ALg8bN6BzNUiHbwxzTXagtT2bHWbJzQ5SoJYNQIxfXTQX0ReoAPIzDSQwlgm9bng+QiqzALgr0639glypB2G0owwaho= X-Received: by 2002:a5d:834e:: with SMTP id q14mr16407682ior.258.1547478042904; Mon, 14 Jan 2019 07:00:42 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 14 Jan 2019 16:00:23 +0100 Message-ID: To: Rowan Collins Cc: Craig Duncan , Internals Content-Type: multipart/alternative; boundary="0000000000000b8e22057f6c4f06" Subject: Re: [PHP-DEV] Making stdClass iterable From: nikita.ppv@gmail.com (Nikita Popov) --0000000000000b8e22057f6c4f06 Content-Type: text/plain; charset="UTF-8" On Mon, Jan 14, 2019 at 3:48 PM Rowan Collins wrote: > On Mon, 14 Jan 2019 at 11:57, Nikita Popov wrote: > >> Rather than implementing Traversable, I think it would be much better to >> implement IteratorAggregate and provide a proper getIterator() >> implementation. > > > I think adding behaviour to stdClass is a precedent we should approach > with extreme caution. > > Currently, it has no methods, and matches no type checks other than itself > and "object"; in a sense, it doesn't even have any properties, just the > ability to add them dynamically. To me, that makes sense: it's the > "classless object", the absolute bare minimum any object could be. > > > >> What I mainly have in mind is that >> we should stop allowing iteration over arbitrary objects in the future (we >> did the first step by excluding them from "iterable") and instead require >> the use of an explicit PropertyIterator, or similar. > > > I agree that this should be explicit, and it could probably be implemented > as a very light wrapper. > > > >> However, we'd probably still want to allow direct iteration of stdClass >> > > Why? What do you consider the role of stdClass to be, that means having it > opt in to this behaviour would always be appropriate? > The way I see it, stdClass is basically an array in object notation (and object passing semantics). It carries data in the form of key value pairs and has no associated logic. The only things you can do with it are access keys (properties) and ... iterate over them. Mind you, stdClass is an anti-pattern. Instead of stdClass you *should* just be using an array. We should never have added stdClass. But now that we do have it, and with the way it is typically being used, I think it's fully appropriate to iterate over it. That is much unlike most other (non-Traversable) objects, where iterating over them is only reasonable in a few rather specific scenarios (say, a serializer, cloner, hydrator or whatever -- something that explicitly is interested in inspecting properties of objects as a matter of representation). Nikita > If you want something iterable, you can convert your stdClass object to an > array, wrap it an iterator that accepts any object, or convert it however > you want into a "proper" object where you can define what behaviour you > want. > > Regards, > -- > Rowan Collins > [IMSoP] > --0000000000000b8e22057f6c4f06--