Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107822 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57482 invoked from network); 18 Nov 2019 20:03:17 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 18 Nov 2019 20:03:17 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 5F5682D1FC9 for ; Mon, 18 Nov 2019 09:55:58 -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-xb32.google.com (mail-yb1-xb32.google.com [IPv6:2607:f8b0:4864:20::b32]) (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 ; Mon, 18 Nov 2019 09:55:57 -0800 (PST) Received: by mail-yb1-xb32.google.com with SMTP id d95so7532559ybi.8 for ; Mon, 18 Nov 2019 09:55:57 -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=CCxMkzYw2lN90oj1VuVNqHfHNiGUMDSEedJobDBob2s=; b=NpbWJliavHJaatggnQhwuPRy1ESUBqG7aJIO9n+ELpmi6nRIKioj457u9f56hDtUk9 LCXzT/owCFuJHlOFvwmLE3paPt7q7INiqW1O8QuMurZ2hh+G1M/56pMEuB8mL82P2Mn8 HQqaRuhxbyTv0/J0CF+eyk58ECabFPuBEQNuMj/z2ux+ohBu7psZiwwPfmcMUwpXt8kw 4+wCF49ehLxKkOQnp72mZTXMf61TgMAtYhLuj4lAi8vvj+vHYLcFy572dvLgo12GBVk1 C1d80ZPFGEPM9xFabwt4sX1b3vmnWtcBEeKDepY9lwXC9yBZ7fBCaLZ3rs4vUJCIRrAa PeXw== 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=CCxMkzYw2lN90oj1VuVNqHfHNiGUMDSEedJobDBob2s=; b=LrkNrhRdTOSzgaXuhL5wMRavoGAevOUot5i1zypg04E4nOjngN9rOUxSFN70ReIGcI 0jAUnzaDlJ9EOLAQxqiByw8GOi5cxkISN/CHrfHNTbIRWWrixMdtZFuZ1vbVnsmaC9tH p6GHLBKlybJGjEVaQyXz4hHRxgYDiPO93/MihonEgy2huakVLVMwGAqJgZdLQgdG44lE VzFRZcISDsIQf0yNAro+xVro2Agt7Xz40fm2xm4ayXES+G2tpzGdtI5D+euCniULQPwm PN7nAnEZeY/5L4Sl9oHu56Zga9KK2SO/WSnMAKwlJzy7FtcStRyqPm2P5j8y2u2oRzZU XUlQ== X-Gm-Message-State: APjAAAUGQWn+YRxTU1Wk4CfLq0c8sRH2aPdYu8kpVlT1AcXWJD+VRzi5 xXUXPZwf5ecldN8TkZlE0vJtug== X-Google-Smtp-Source: APXvYqyqOe8pF03GQzG2V1s+gpVKnlOuhgLn0VgOTgkH2mQr9FxaRgV4oTUKcJTfZf6qZKZiGn6j2g== X-Received: by 2002:a25:71c6:: with SMTP id m189mr23508545ybc.394.1574099757143; Mon, 18 Nov 2019 09:55:57 -0800 (PST) Received: from [192.168.1.10] (c-24-131-44-78.hsd1.ga.comcast.net. [24.131.44.78]) by smtp.gmail.com with ESMTPSA id l34sm3164838ywh.40.2019.11.18.09.55.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Nov 2019 09:55:56 -0800 (PST) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_D8B48CCC-D342-4397-A28C-4797910FE6FE" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Mon, 18 Nov 2019 12:55:54 -0500 In-Reply-To: Cc: PHP Internals To: Rowan Tommins 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=_D8B48CCC-D342-4397-A28C-4797910FE6FE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Nov 18, 2019, at 5:53 AM, Rowan Tommins = wrote: >=20 > On Mon, 18 Nov 2019 at 00:18, Mike Schinkel = wrote: >> Even if short, a code base littered with method names like toV5Json() = or >> getV5Array() or formatForJsonV5() is still inconsistent. >=20 > Inconsistent with what? Inconsistent with each other. If one developer names it toV5Json() and = another developer names it getV5Array() then those two developers = choices are inconsistent.=20 Yes, a team lead could require an interface be used for consistency = across a team, which is fine, but it is not consistent across unrelated = projects. Having worked with GoLang for a while where interfaces are not = required to be explicitly named by classes that implement them I have = learned that having consistency across unrelated projects can result in = some surprisingly serendipitous reuse scenarios. > Unless you suggest we introduce enough magic methods that every method > name in your application begins with two underscores, you are always = going > to have to name things. Forgive me for saying, but that argument is bordering on reductio ad = absurdum. We are talking about a magic method for converting to a = built-in data type.=20 In PHP =E2=80=94 ignoring null =E2=80=94 there are only seven built-in = data types, we already have a magic method for one of them (string), = having one for resource makes no sense to me since it is such a special = case, and so the only ones for which most instances of a declared class = can be fully captured are array and object. IOW, two (2) potential = magic methods: __toArray() and either __toObject() or __toStdClass(). That said, I could see a value in having __toInt(), __toFloat(), and = __toBool() as having them would allow us to represent scalar data types = in classes. That could be extremely useful in selected contexts. So given the argument there would at maximum be five (5) new magic = methods that could be added vs. the infinite methods you assert this = approach would invite. P.S. That said, I could see adding more for well-known data that goes = beyond existing data types, e.g. JSON, XML, HTML, CSV, etc. but really = if we did that I would argue that PHP should create a first-class = built-in data type for them first. > If there is a common requirement for objects to be converted to a > particular type of array, then you should pick a standard name for = that You assert that a developer "should" do it the way you believe is = correct, but what is the arbiter of "should" vs. "should not" here? Is = there some fundamental best practice that the industry has universally = embraced that I am unfamiliar with, or just your opinion? =20 (I am not trying to be sarcastic, but I am challenging you to justify = your use of "should.") > I'm not asking for any kind of perfection, I'm just saying names = should be > meaningful. Bear in mind that namespaces are really just part of the = class > name, so all you're really saying is that you like long specific class > names and short generic method names. Absolutely agree that names need to be meaningful, which is why I want = more control, not less. =20 What you are implicitly saying is that rather than allow others the = option to use __toArray() which by its nature could result in consistent = use across unrelated projects you want to deny others who value it from = being able to take advantage of it because you personally do not see = value in it and would not use it. Or did I misrepresent? >> Similarly it is not clear to me was toV5Json() or getV5Array() or >> formatForJsonV5() mean. >=20 > I was imagining an API that had gone through different versions, so = needed > methods to serialize objects into the JSON published in version 5 of a > specification. Godforbid someone uses that kind of naming for their methods. Versioning = screams out for encoding into namespace names, not method names. Why = burden the developer with the requirement to use the version name in = every single method call? Better to isolate it to use a use statement. BUT, this is a tangent and a different debate from the discussion about = __toArray(). > If your code base is such a mess that you can't propose any method = name > without it conflicting, and can't trace usages of any method that = needs > renaming to not conflict, then yes, you get a one-off benefit from the = fact > that __ is reserved. That seems a pretty poor justification for a = language > feature. I was not talking about *my codebase*, I was talking about the = collective entirety of all existing PHP codebases. So I now understand that you were proposing even less than I thought you = were proposing. I thought you were proposing that PHP would recognize = and give speaking meaning to a method called toArray() or similar. Now = I realize you were proposing even less and instead placing the burden of = said standardization on each project, guaranteeing no serendipitous = reuse across projects. :-( > One huge benefit I haven't mentioned yet is the ability to add = parameters. > Imagine if your mustache formatters are used in both plain text and = HTML > contexts, and that affects some of the data to return. When you have the need for parameters, by all means create a named = method. But why block the use cases that do not need parameters? Why = not simply let developers who find __toArray() useful be able to use = them? > If you have a > Thinking about it, if you're only going to allow one method per class, = with > the class's namespaced name telling you what it does, you might as = well use > __invoke(): But it is reasonable to expect that a class could need both an = __invoke() and a __toArray() method.=20 Also there is no guarantee that an object that implements __invoke() = will return an array whereas there should be a reasonable guarantee that = if function_exists($object,'__toArray') is true that casting to (array) = would be valid. > You can only rely on __toArray if you standardise on every object > implementing it; at which point, you can standardise on any name you = like. But method name standardization does not work across unrelated projects. = Because programmers. Casting to (array) would work across unrelated projects. > People are quite happily using polymorphism with named methods in = literally > millions of OO code bases. https://en.wikipedia.org/wiki/Status_quo_bias = > I'm not suggesting *PHP* standardises on some other name, just that = *your > code base* standardises on common names for common tasks. And therein is where your argument breaks down compared to the argument = for __toArray(). It assumes each team's and/or project's codebase is an = island. > Well, I started this sub-thread saying I was "sceptical" rather than > "strongly opposed". If the feature was added, I would simply ignore = it, and > probably argue against its use in code review and style guides. I think I have discovered another difference between you and I. When = someone proposes a feature I can easily ignore, I do not write long = emails arguing against it.=20 #justsaying :-) > However, other people have pointed out that unlike (string)$object, > (array)$object does have a default behaviour, and adding an overload = for it > has the potential to break code relying on that. So it's not an = entirely > zero-cost feature. I do not see this as an issue because this BC break is entirely opt-in = by the developer. It is possible a library developer could add a __toArray() in a new = version of a open-source library where one did not previously exist and = thus break other's code that depended on the previous version of the = library. That would be no different than if the library developer = changed the behavior of one of their existing methods. So that would be = one the library developer, not on PHP.=20 -Mike= --Apple-Mail=_D8B48CCC-D342-4397-A28C-4797910FE6FE--