Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103735 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62030 invoked from network); 14 Jan 2019 18:33:51 -0000 Received: from unknown (HELO mail-yb1-f176.google.com) (209.85.219.176) by pb1.pair.com with SMTP; 14 Jan 2019 18:33:51 -0000 Received: by mail-yb1-f176.google.com with SMTP id t16so8726209ybk.10 for ; Mon, 14 Jan 2019 07:09:31 -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=95prRZRjlOUIq586pO/+Cm4J197lfyKktrxZm08OXHA=; b=qD8z4UXs3fMWhV+46G75H4OKhst7hv7LLTmHA7PyGlZE7YtsPvsnZTYjGfZioBbc6l +JyGNbLjEwCNl8Bqr7tlMq5HOLqNxprTuG0zDihxwHGO8xe/fc5LWDEiYt/ItyK7y1NT FCV/3bzp/S6nYAjOiUltMjgtSmc7V0Hhjs9EbTTJ/WQxgVBMz9aIUFn4Owpjd0vdi6HT hEb+IkcqNBLGdNJ7W340mJT01z5LDbL2pmyztBhW3hIr97m2j1v3HqDqfthdMyu2Fc8J gFFcJUUijGL2kcnPDj2apJjuPKWo82dIabMiq+IfRpbbV7X4eNDjyqFct8sagAHOMTLL AZpQ== 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=95prRZRjlOUIq586pO/+Cm4J197lfyKktrxZm08OXHA=; b=LZda165nt/mP5cGJMhjDz6WWAgor+WdmAxL6OBMppClO0jenOGXxH6NjXBrXbvbKu4 u4h4a/xmBX8oYGKk1XxX7VPRzxDrKr9WM4zrEhfhm3YcoZKLQzAP2JRRh20otkYy4UKN YyLPoBvfv/kk/SwCZuaCTa4bw8TstvCoDiQI0GamtLIDxw8WmfpHL8D+BCWqUCVUAGzD AZrT764QeA3JvfvG7pBGnzY5R7FwAet+Dm7iXF7y6jbdIt8GFr4aLuhMHnTjJNS3fQXU QcHfotvm9jD0b09/a/aPf8fS04/G9x8NX8iSuAnOFDIkklYd0A2vKChcnyZRZbsFj0VV hvDQ== X-Gm-Message-State: AJcUukdIox/7rK09PE8DrFLx0CuSXE62GpOuH6vt5AzWjlvvSCPdbdnU 0+KmjzRWggL0XNh0ia/AjTph9Wqn07rEONiZq9o= X-Google-Smtp-Source: ALg8bN7FC4Gqu3bHROvls4XEU2oqaBBz9ltvNFjQQWS7MBdGOqPN8hK/qVrx65xLP1yOLqASpm5JtTuuIHNa8sNsjsI= X-Received: by 2002:a25:8287:: with SMTP id r7mr16965850ybk.246.1547478570513; Mon, 14 Jan 2019 07:09:30 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 14 Jan 2019 16:09:18 +0100 Message-ID: To: Nikita Popov Cc: Rowan Collins , Craig Duncan , Internals Content-Type: multipart/alternative; boundary="0000000000007e38bc057f6c6e3b" Subject: Re: [PHP-DEV] Making stdClass iterable From: benjamin.morel@gmail.com (Benjamin Morel) --0000000000007e38bc057f6c6e3b Content-Type: text/plain; charset="UTF-8" > We should never have added stdClass. Why not deprecate it then, instead of extending it and giving it more legitimacy? This would remove some of the "2 ways of doing the same thing", such as json_decode(). Ben On Mon, 14 Jan 2019 at 16:00, Nikita Popov wrote: > 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] > > > --0000000000007e38bc057f6c6e3b--