Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107812 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10802 invoked from network); 18 Nov 2019 01:09:20 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 18 Nov 2019 01:09:20 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id C0B712CC1C2 for ; Sun, 17 Nov 2019 15:01:50 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Sun, 17 Nov 2019 15:01:50 -0800 (PST) Received: by mail-yb1-xb31.google.com with SMTP id i3so1445638ybe.12 for ; Sun, 17 Nov 2019 15:01:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=xqrrbDWnbSsE6ZNWT3RkafjJHjGFFBZjg0GANQUgLlc=; b=v1v2WmDrOTZg0yDmg1GqSfkkdz6TTEaa2qRYimCurHRMXXjV9pImTwFMb/JFYq1A1e 8RLQDWqkpxOLyXtcJuD4/Ho3lUC8VHXD/brQnZvXJRWxUWVAAhIa1adghPj5MckMoWCl Hi4LqxiXFAyHi09ykt1N+welh++gIZ+PvEt2Yy9gP26bJdBA0LeU0Xmb1N8TkHxI9z4K VqB9C/dKcfKFLMmHiELPKWaAgQoU/O2MHtycQW5xreC7fvBPZk6hfKlgYkbWz1PqB4iY OPFYTYJ/cjq8FLMi30AXDruOSI9DaXUuLMeFy9uTl3qYapNKx6wlp2zHbnXpkIX9Fkov TgUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=xqrrbDWnbSsE6ZNWT3RkafjJHjGFFBZjg0GANQUgLlc=; b=SgU9TRzrP7z/0gsMVRLRh1Y5N/yqRTUTvkM7wi9YcjrO/3Ug2yGAZgiFJ7k8zEeiIX ggwSZ+Rlx+6AvgydqG2fO25VyVpSwWN0za8as3KnlOUp3x9iccJcOGLTEtARviiWoKde C/rrlFu6w6vvBIHyA6dII96afMX4xA8a2DnledV3rg6orFeShqvP3tyDV90nB4C2dJ3W zqTm59aABjWkUErqaj07+5az/vePUaev89cZ+SuwQ/KTVjbMO8UCoQQ0akVDrdZ45imK ZFGw+jgGXbfp4MMhayHufMoZxAYZfGMUNmj67Cng0OBGiVXR24EYGOIlCRD2QYi0+r9U O83Q== X-Gm-Message-State: APjAAAUD4gtWXzpGliea4hQlX29fwZTV6XFOwy3+xhg8AkjClyUZ1EhV bTsO3RmcHc9wnT89NVsKWF1pyQAf1g0s2w== X-Google-Smtp-Source: APXvYqzEC3JEVwahcnvB2CmKtRqcwEjl1N7dq9/6LaoHwF6g7klB8oaF7+zB3tqszMyMHBCpoTHmtQ== X-Received: by 2002:a25:c281:: with SMTP id s123mr21106992ybf.92.1574031709109; Sun, 17 Nov 2019 15:01:49 -0800 (PST) Received: from ?IPv6:2601:c0:c680:5cc0:3536:e373:d5ec:26a8? ([2601:c0:c680:5cc0:3536:e373:d5ec:26a8]) by smtp.gmail.com with ESMTPSA id j66sm7024446ywb.101.2019.11.17.15.01.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Nov 2019 15:01:47 -0800 (PST) Message-ID: <1BF6C8EC-6DF1-4481-94B2-3A6E7D1312BE@newclarity.net> Content-Type: multipart/alternative; boundary="Apple-Mail=_8E716A27-8F02-4D71-864F-4738263F1893" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Sun, 17 Nov 2019 18:01:46 -0500 In-Reply-To: <7b527bbb-47e3-9b24-8766-c15e6334c78a@gmail.com> Cc: Rowan Tommins To: PHP Internals References: <461958fe-182e-59bf-9fdb-b110fd09e2b2@aimeos.com> <08de5448-3c5a-ad32-555e-b8f9579c1337@aimeos.com> <7b527bbb-47e3-9b24-8766-c15e6334c78a@gmail.com> X-Mailer: Apple Mail (2.3445.104.11) X-Envelope-From: Subject: Re: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable interface From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_8E716A27-8F02-4D71-864F-4738263F1893 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Nov 17, 2019, at 2:27 PM, Rowan Tommins = wrote: > Well, pretty much any method that returns array *could* be called "to = array", but not many would be good candidates for such a generic name. = You might return an array structure to be incorporated into a JSON = response, or to be passed to a template renderer, or to map to database = columns, etc. >=20 > The only case I can think of where a generic "toArray" method would = make sense is if you're creating a general-purpose "collection" or = "list" object, in which case you're probably better off directly = implementing methods like map and sort, rather than encouraging users to = convert it back to a plain array. It doesn't seem like a common enough = use case to need a new language feature, when it's simple enough to = write "$foo->toArray()" without anything new. I have been pondering that objection ever since Steven Wade proposed = __toArray() on the list a few months back. =20 In part I agree with the sentiment, and in part I feel that perfection = is being the enemy of the good here. But as I had no strong argument = for how it could be done better I did not say anything on the matter. Consider this: __toArray() is hardly a rare case where a short name can = be applied in multiple contexts. We have infinite contexts where we = need to name methods for one context that might conflict with others, = and so I have been agonizing for years over how to deal with this = conundrum without resorting to really long method names.=20 (As an aside, I think long method names result in harder to comprehend = code, and as I have never seen a set of conventions that results in = multiple programmers (almost) always choosing the same long names for = the same use-cases I prefer our standards to suggest shorter names so = that we can maintain more consistence across programmers.) Recently I have been experimenting with using namespaces instead of long = method names, and I think using them can result in the best of both = worlds and resolve your concern. Consider the following classes, each = of which could have their own __toArray() method specific to their = use-case: \Widgets\Widget \Widgets\JSON\Widget \Widgets\Mustache\Widget \Widgets\DbColumns\Widget With the above you can "have your cake and eat it too." Your namespace = can specify the exact context for your __toArray() and thus developers = who want to use that architecture can get the benefits of a __toArray() = magic method. #jmtcw -Mike= --Apple-Mail=_8E716A27-8F02-4D71-864F-4738263F1893--