Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107816 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33431 invoked from network); 18 Nov 2019 02:26:13 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 18 Nov 2019 02:26:13 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 5998A2C1ADF for ; Sun, 17 Nov 2019 16:18:44 -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=-1.9 required=5.0 tests=BAYES_00,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-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) (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 16:18:43 -0800 (PST) Received: by mail-yb1-xb35.google.com with SMTP id g38so6461179ybe.11 for ; Sun, 17 Nov 2019 16:18:43 -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=ELgv5eYYIXUtHGGpisAAr1/UOg2ZgH6GR6jxA/9ZscM=; b=INfbsPa2HbfkbRcCTnR3nSAtN1Ja3R9c43evNLB2NLsh9icNfBD4PzJ6thJGA0XLWl HZFdTFeXQSOGX5KXaMhK827m93vhCUsxq4lARcFqxt9cCaoQ8gc9TVXzB+bhzwlsAZ/e f0Aznr3HoRpiR+xiZk8/wb5Qlp6g5lU27dP8neeOSVmrREzdpShxFoQK+l3qIxyY7rG3 El+zZ9PpSxERt3M7jxMXjyxRQSBn3Eq54bSM2dI6Hp0IGeb7NbGJeLMTfSslo3GXbvGm OWoK57fpH+99ByFVwlQ69Lq8n7eaY1iC7wcob4Vf9JGRxhPWEI7sm7IHbPvidULZD/BS wu7g== 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=ELgv5eYYIXUtHGGpisAAr1/UOg2ZgH6GR6jxA/9ZscM=; b=b6Wm9WEidS3wChRsLej/47tYI7gWj/Cm0nt+Vwg99gvJtAx7GK7rT+mWgHTJEXW8pK j7zWV2VDuGSkugXczkA2VabsgeFKQ3ClbX1SPkOZUX83z44Kfb6tV+cRcOIzzLFopByq GG8L+NHmD5CVfLnAxTfPc91ZD4UIdXoxKlrB0+YOpEJYclYJJxSShhEMtRmq0gVeGK5P A0Cf7QSe+dABqrPrwjDmyXAv/lDxKyEZF0Qnpny3YgNG0/vquHL9u27s49hl3bKXFJl7 6x+xK2Fl354Ns4q1t1MpLUtcTLfPA3sjdj7QmJXMIj+2rcEErXZ1Iw0cihG+AB9hNPLb uA9w== X-Gm-Message-State: APjAAAX2RGnQqLcAQmAK196g8KiWtc4FUck5rmWkP5IM7tatTgaNVCC2 QCkIMkR8xdm5kRw05FOzcHuslxfIY6grdA== X-Google-Smtp-Source: APXvYqxRJa75h2TLpQ6CODnnC9Gepbb/ydqO0NF87SC4Gl/YZ1K4lB3Wy9DTOUHXTvurWIPPV6qusQ== X-Received: by 2002:a25:ac19:: with SMTP id w25mr20664064ybi.400.1574036322843; Sun, 17 Nov 2019 16:18:42 -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 17sm9751301ywb.13.2019.11.17.16.18.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Nov 2019 16:18:41 -0800 (PST) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_142ADE1C-C0CC-46CA-83DF-3A03D6694901" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Sun, 17 Nov 2019 19:18:40 -0500 In-Reply-To: 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> <1BF6C8EC-6DF1-4481-94B2-3A6E7D1312BE@newclarity.net> 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=_142ADE1C-C0CC-46CA-83DF-3A03D6694901 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Nov 17, 2019, at 6:51 PM, Rowan Tommins = wrote: >=20 > I'm not sure avoiding the name "toArray" necessarily leads to "really = long method names" - even with extremely specific distinctions, you = don't need to call the method "toJsonArrayForVersion5OfTheApi", just = "toV5Json" or "getV5Array" or "formatForJsonV5". I now realize that my commenting on my experience in reviewing legacy = code =E2=80=94 where long names are frequently used, regardless of the = fact they are not required =E2=80=94 caused you to focus on the long = naming comment aside and not on the primary ask for consistency. =20 Even if short, a code base littered with method names like toV5Json() or = getV5Array() or formatForJsonV5() is still inconsistent. > The important thing is that the method name is now communicating its = *purpose*, whereas "toArray" communicates only its return type, and a = hugely flexible return type at that. What I was suggesting instead, which obviously was not clear, is that = you communicate the *purpose* with the namespace and by doing so allows = for the capability of casting to array be added. Otherwise the perfect = is the enemy of the good. > I'm not clear what these objects represent. If I have a Widget object = passed out from some business logic, Similarly it is not clear to me was toV5Json() or getV5Array() or = formatForJsonV5() mean. However if V5 somehow has meaning then your namespaces class could just = as easily be named if that is what you want: \Widgets\ToV5Json\Widget > how do I make use of these other classes? Would I have to call = "(array)(new \Widgets\Mustache\Widget($myWidget))", as sugar for "(new = \Widgets\Mustache\Widget($myWidget))->__toArray()"? Maybe we code differently, but I would never write a full namespace in = code like that. I would have one of the following at the top of my PHP = file: use \Widgets\Mustache\Widget use \Widgets\Mustache\Widget AS MustacheWidget=20 That would result in the following as one of =E2=80=94 a you say =E2=80=94= sugar for (new \Widgets\Mustache\Widget($myWidget))->__toArray(): (array)(new Widget($myWidget)) (array)(new MustacheWidget($myWidget)) However I doubt I would ever instantiate and then cast the object in the = same line of code. Instead the value of array casting =E2=80=94 to me = =E2=80=94 is in its polymorphic nature where I can have a "generic" = method that can just cast to an array and instead of having to know if = the developer used toV5Json() or getV5Array() or formatForJsonV5(). > If so, I don't really see the benefit of the magic method over just = standardising a method name, like "interface MustacheFormatter { public = function getData(): array; }" Backward compatibility is the benefit. If we standardize one any name = then we can break existing code. Adding a magic method cannot break = existing code unless that code violated the reserved nature of double = underscore methods.=20 Or are you proposing we start adding __*() methods that are not actually = magic? > Which is basically my objection to __toArray() - I can't think of many = situations where writing (array)$foo saves or gains you anything over = writing $foo->asArray() or $foo->somethingMoreSpecific() But I and others can think of situations where it would help.=20 Because asArray() and somethingMoreSpecific() are neither a standard you = don't get polymorphism with different named methods.=20 And any new standard non-magic method we add has the potential to break = BC. And is unlike anything else in PHP I can think of. OTOH, I have a hard time thinking of a scenario where __toArray() would = actually cause a problem for someone like you who cannot think of a = benefit. Can you present a use-case how it would cause you a tangible = problem that could not be resolved with namespaces or by just creating = your own somethingMoreSpecific() method and ignoring __toArray()? -Mike= --Apple-Mail=_142ADE1C-C0CC-46CA-83DF-3A03D6694901--