Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109105 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 76111 invoked from network); 17 Mar 2020 16:08:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Mar 2020 16:08:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E1967180555 for ; Tue, 17 Mar 2020 07:31:02 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 17 Mar 2020 07:31:02 -0700 (PDT) Received: by mail-io1-f43.google.com with SMTP id c25so20584774ioi.5 for ; Tue, 17 Mar 2020 07:31:02 -0700 (PDT) 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; bh=xiliqqSk9Epnym+KdYPODAllqdj/1YFdQ122ZTZbyIs=; b=YCsvn6YS3+TmkbJJNkAJaA4eL444IwJmic+ByJj6v09A259w7uau+kjWgPU941KlxA Bot0hafAxi0E0xFF2r31C6rWR/OE150U5vOxGE9A010WDmo/SckzHOgAcKb1/gIPL1Bx ++fOvWLxy/eTqMY7EWZUZOtl+uZKR4d9Gjuu/MgjwDzJdWIOYzu5jkORAzJ8WOOranDl aGC4LXOnbZoNGOFrOCKQXdfyqZrlUeey3t0evK0EyelG5O8+ARvOKJPL0oW88M1Y501u 0E+fd9rnLm+//zwHmR+KfgvmhitjcKx8CdmY14QP5JzvtQlTyPFan9jedZxyqANYNdri OrKQ== 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; bh=xiliqqSk9Epnym+KdYPODAllqdj/1YFdQ122ZTZbyIs=; b=qrl9T2oy5N5veFnAPi2NLGZx9aFknFAydwJqCsIdjQSTlyh9wp4wLyd87T92Uln2ET bSPHuzXMlR0K2ySt704CljBFymDvnAm3wzXwvHyQUHdsPU7zZp5tgSraCqPRWSVx8Kyy jr0rmatExWoYx9GTpzxlS7lDlnfL4+u0UZtLVtg/Akl2XfTKwJGWvQVonAAwpNC5UeTV jmx0TgQ5KVSf5U+LEmuUfo3vljicO2sEcNpx95jwFGAEgG0aRtYjOzhrGZmMq5I7jCgk JtW8fJGuxvbvR2Jvoe88KEQdylxPz+gEzqRJqy1XnjpkhpU2gQJNuIZYVEa05IrQMDT1 mazA== X-Gm-Message-State: ANhLgQ36dXWC4OzkXDVs4cJlngjuo3BXsIDDIpSJPWEzK+QzuNteo0F+ MTGl3uXhuz+7Noe8nUoSPs7EAo7vrsboRh32klo1l3ZN X-Google-Smtp-Source: ADFU+vvK7FBDwdjhnwn7hf9dJKkIwrJljcz0vDXrbfhzMNojfHBIhTQkqtZtB+ULtzAWseTE6S5ypYXosvXedvnUdcw= X-Received: by 2002:a02:3b4a:: with SMTP id i10mr5639765jaf.131.1584455456961; Tue, 17 Mar 2020 07:30:56 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Mar 2020 14:30:45 +0000 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="000000000000acb9ac05a10dc88c" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Compact Object Property Assignment From: rowan.collins@gmail.com (Rowan Tommins) --000000000000acb9ac05a10dc88c Content-Type: text/plain; charset="UTF-8" On Tue, 17 Mar 2020 at 01:47, Jakob Givoni wrote: > Remember that the RFC explicitly says it's not an object initializer, > nor does it solve "named parameters" which you mention. > I wasn't expecting COPA to "solve" named parameters, just thinking that if we already had named parameters, we might not bother with COPA. That said, your "argument bag" example looks very much like it's trying to solve named parameters to me. > An Options class add almost no extra boilerplate since you simply move > the properties from > the main class to the Options class. > Notice that in contrary to your example, I simply copy the Options > object whole into the main class. > Hm, I see, that does reduce the boilerplate somewhat, although it's still split across two classes, and probably therefore two files, which is not great. You're missing some code in your example, though, because you've documented one of the options as optional, implying the others should be mandatory; in which case you need something like this in the constructor: if ( ! isset($options->mane) || ! isset($options->hum) ) { throw new Exception('Bad options'); } Which would soon get longer with more complex options. Maybe it would be refactored out into $options->isValid() or something, but it would have to go somewhere, or you'll just get "uninitialized property" errors in random parts of your application. You'd also want to copy the property onto the real object if it was initialising state rather than retaining a constant value; it would be a bit weird to write $this->options->counter++ instead of $this->counter++ > > > COPA can only validate individual > > parameters, not the whole object; so it's hard to complain if the user > > forgets a mandatory field > Correct, the goals for COPA are quite clear in the "Motivation" > section and that kind of validation > is not one of them > Luckily you can still do things the way you prefer without COPA if it > doesn't suit you :-) > It's not so much about preferring different styles, it's about what circumstances this will be useful. It seems pretty rare that an object would have no mandatory properties, so saying "if you have a mandatory property, COPA is not for you" is ruling out a lot of uses. It might be interesting to have the syntax run before the constructor rather than after, or trigger some other magic method which could do tidying and validation, so it could apply to more circumstances. Regards, -- Rowan Tommins [IMSoP] --000000000000acb9ac05a10dc88c--