Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:107080
Return-Path: <rowan.collins@gmail.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 39611 invoked from network); 13 Sep 2019 17:00:06 -0000
Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12)
  by pb1.pair.com with SMTP; 13 Sep 2019 17:00:06 -0000
Received: from php-smtp3.php.net (localhost [127.0.0.1])
	by php-smtp3.php.net (Postfix) with ESMTP id 49FBD2C0499
	for <internals@lists.php.net>; Fri, 13 Sep 2019 07:36:15 -0700 (PDT)
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net
X-Spam-Level: 
X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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: No
Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36])
	(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 <internals@lists.php.net>; Fri, 13 Sep 2019 07:36:14 -0700 (PDT)
Received: by mail-io1-xd36.google.com with SMTP id b136so63049533iof.3
        for <internals@lists.php.net>; Fri, 13 Sep 2019 07:36:14 -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=3/QhWLuwgjZLWX8gvB7dtoLg/j/HaQ60m+VpIF7W5BQ=;
        b=lHyEDt7nZ7tb8tZWBVWxGSQzcm2tb8B9nmtq3m7C25tAfmdXjaFOdWxkF5hFGzOzv2
         HnXYLPidnILgB8QxmRxLpvopHDIt65MIB5QZk8knwxOgThnQE2sAbf74sAGfWP997tef
         POn5M5P6AsXuOFIEqSQ4E7IGja1AyG3q981m0LY8DZZ+5y8V5KiCSryHAv225fWP3nhc
         bVfRJXuoGMHkEsTzE1J6NUtdeIIjadgq7iECzgtkrfDMrnyZm18UmsR3vY2G7gXt2JKV
         pNTEokchehREcE6IBR/bFvgfbHr31oZTXTkC6S+gWer59/AkN/WEo93CorXknBan2wqv
         rn1g==
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=3/QhWLuwgjZLWX8gvB7dtoLg/j/HaQ60m+VpIF7W5BQ=;
        b=KNbAZugjs5YGutREJNhDPbBPLgqg18AkE7xzu+TRJFxtIdsT4gKSeI/B1ug0tSdFGH
         ndqnXXzVqSulExDC6ZW54LGaxMHEVAa0GnAJUETBNdh1G1N/kVI1ynt1HMwbX5O5BL5O
         UywOl7wbfzRKoEXD/+Hut5/BxJHCpUbIMEYGS10GkDfEY+aOP8gMUVLOf2eCDQvJE5eN
         Y7tSWtQa70kZV+et8mSrg7hdRIWFvQiBG5cENvmyJKAQ/fy/6/BHCl7+HRIfB/bWESCN
         MBwlkAJw5mkbBEZh/6m3TPcFQ98EUBMcio4dh8h3muwNaBG4fssWxCAZFEp/byRLzy8e
         Bd9A==
X-Gm-Message-State: APjAAAW7mgSCpT8771uwN6zjHBrPAnAKnyKJc1GmtXAW7oUCPYhMPbJN
	0xEIFYvi7m3h6ITu38k4F0uLo3wdhxUraau/3o2JAl6M
X-Google-Smtp-Source: APXvYqxzaeJFMXavrjvMpIoYNBrl3zh7m9ZUkz46uHQ5taXSoL7OZ+cRbwGxE2Y92i/2enfp4aNNy4nWGhN95byH284=
X-Received: by 2002:a02:9f16:: with SMTP id z22mr49509964jal.83.1568385369107;
 Fri, 13 Sep 2019 07:36:09 -0700 (PDT)
MIME-Version: 1.0
References: <CABdc3Wpit02=BqvVE85ciw9qo3UYc2LBatOy23yFk5z+quZ2rw@mail.gmail.com>
 <CADqTB_gwv48eGO7tBLQkQ77BqUA0yJL_QG+DrY+nPzneXDTsvA@mail.gmail.com>
In-Reply-To: <CADqTB_gwv48eGO7tBLQkQ77BqUA0yJL_QG+DrY+nPzneXDTsvA@mail.gmail.com>
Date: Fri, 13 Sep 2019 15:35:57 +0100
Message-ID: <CALKiJKqkBUxFOUYvaCnRBoczoRZtnTsZLmDCJwurkgvZ3RdiBA@mail.gmail.com>
To: PHP Internals List <internals@lists.php.net>
Content-Type: multipart/alternative; boundary="000000000000cd01d80592702ca9"
X-Envelope-From: <rowan.collins@gmail.com>
Subject: Re: [PHP-DEV] [RFC] Object Initializer
From: rowan.collins@gmail.com (Rowan Tommins)

--000000000000cd01d80592702ca9
Content-Type: text/plain; charset="UTF-8"

Hi Rasmus,

On Fri, 13 Sep 2019 at 11:18, Rasmus Schultz <rasmus@mindplay.dk> wrote:

> All in all, I find this feature is useful or applicable only to a few,
> select patterns within the language - it isn't general enough.
>


I've trimmed the quote for readability, but agree with basically everything
in this message. :)

I like the reasoning behind this RFC, but think it is unnecessarily
limited. It's not about "public properties are bad", it's just that if we
can come up with a feature that works for public properties *and* other
programming styles, we should prefer that.

A related proposal that's come up before is short-hand constructors:

class Foo {
    private int $foo;
    public function __construct($this->foo) {
        // automatically assigns first parameter to $this->foo so body is
simplified
    }
}

Combine that with opt-in named parameters, and the Customer example from
the RFC might look something like this:

class Customer
{
  private $id;
  private $name;
  private DateTimeImmutable $createdAt;

  public function __construct(id => $this->id, name => $this->name,
createdDate => string $dateString = 'now')
  {
    $this->createdAt = new DateTimeImmutable($dateString);
  }
}$customer = new Customer(
   id => 123,
   name => 'John Doe',
   createdDate => '2019-01-01 12:34'
);


It's slightly more verbose, but a lot more flexible.


As a side note, I have always thought stdClass was a bit of a kludge, and
now we have real anonymous classes I would love to see it gradually phased
out. I would much rather see syntax for capturing variables in an anonymous
class declaration than new ways to create stdClass objects.

Regards,
-- 
Rowan Tommins
[IMSoP]

--000000000000cd01d80592702ca9--