Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107169 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60901 invoked from network); 16 Sep 2019 16:09:57 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 16 Sep 2019 16:09:57 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 975A32C8246 for ; Mon, 16 Sep 2019 06:46:51 -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-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) (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, 16 Sep 2019 06:46:51 -0700 (PDT) Received: by mail-io1-xd30.google.com with SMTP id m11so78786326ioo.0 for ; Mon, 16 Sep 2019 06:46:51 -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=tiWUg8WR3Y/DjytNNTzL/SL+wC4fgueLNBazQ4VnWc0=; b=kJykfDjwqsvIIrkHHAGbwUnKeOUP1rMyp/emnuZ1HQnQ9lA0rB4L/cmnytafslkCvw X9kOwdIjw6JS/ITGKY/4U1Ukfm/hEKc7qpVNnR47pvAptQWJZ+ObX46KRguBf9x7TK28 npx4UXSnZYbaygWeuzxKPpOFYXjJHmOqESwpnIA3q4aZcIx9ILuVi32GThNZSjZkhUlo +uR91jPBT49TuzBrlvFSJQhOI0xQj2rr4MO/QfRzyOSseFhQBt2F28fBY2WPAfUHv0q7 dk3niCyLtFjOn70SAaRJOX9/eCSi4nCToV5yYbx5FbI8gxSHt63ryc3EZIu69rYUC9TE a/mg== 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=tiWUg8WR3Y/DjytNNTzL/SL+wC4fgueLNBazQ4VnWc0=; b=D29AZYLbZEoAqxQ3Sy1+7VivzZiOB55Qe+/z5KshEuoZs8fPJMLrafnn9e2Q02n9SG uK9bNg/0Z87F0KcfYpKRRTG+AMkRVc6RuAiKC49b3yJ7LSriq7wCzO1UmG51A8gMyFve kUG8V2MqnTwUN9irHG56Ca/rtYZz97p6BqqC4r7sEP22RuIkW4cwVHwDgGpGPOH1Ogsp CkFbeC7VkzFj31SfVnJkz8mQvbE1avRsQ+TTVMzZJLg8VMR6ZwXag1NJjGJfz6w6hvhe 9nkcWxJxWvJwufyrrX7C5sxodUfSztcEpSTtgm8o0KyhEgT9dVk6gfEthbYX1A4IbX3z +RXw== X-Gm-Message-State: APjAAAX1Jdb+3VKBAsDOVSGJHFGenYDWaQE4ugKn09M7AK4ZTEJRf1Xt bp3u+/v4D2vJPZLQ0u9aa8AjhhKtCLxGpxjr3RLuUTeY X-Google-Smtp-Source: APXvYqyIfdAINy+2ZXZyTvF+3Rus482nOHZVjfgSM7j4fXf5Dn0NW1EmNQSzXHEzpmQhpK1albtjzNZRn5pHDIWSWCc= X-Received: by 2002:a5d:87ca:: with SMTP id q10mr330830ios.305.1568641610363; Mon, 16 Sep 2019 06:46:50 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 16 Sep 2019 14:46:38 +0100 Message-ID: To: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000f736690592abd5c5" X-Envelope-From: Subject: Re: [PHP-DEV] Re: [RFC] Object Initializer From: rowan.collins@gmail.com (Rowan Tommins) --000000000000f736690592abd5c5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, 16 Sep 2019 at 08:29, Micha=C5=82 Brzuchalski < michal.brzuchalski@gmail.com> wrote: > Please keep in mind that initializing properties through object initializ= er > applies to visible properties and is possible to assign > protected or private properties from the class scope as well. > The problem with that is that you need an extra static method to make use of it, and you still need to get the arguments into that method. It might be useful occasionally, but it still doesn't help constructors which are setting a large number of private / protected properties. > I don't see the reasons why many of you consider public properties a bad > solution. PHP language has support for public properties > and with typed properties, it is possible to ensure valid object state > using VO's which are valid all the time, consider Uuid userland > implementation > no one checks if that's valid UUID, cause it's ensured by VO. > Firstly, it's not necessarily a case of "considering public properties a bad solution"; it's about evaluating where the new feature could be used, and where it wouldn't help. If there was a feature which helped this use case *and* other use cases, I think that would be "better", but that doesn't make this feature "bad". Secondly, typed properties certainly make public properties more appealing, but there are still a bunch of things that you can't do with them, like declaring them readonly, or having logic other than type validation in getters and setters. Note that C#, which has object initializers, also has these features, making them a lot more powerful. > Any kind of features like promoting arguments from the constructor or nam= ed > arguments are fine but not efficient in those cases. > Any good practices suggest limiting function|method arguments while the > case is where there is a significant amount of properties > to be initialized and additionally which don't need any kind of validatio= n > due to VO, or simple scalars. > That's a good point; named parameters make function calls scale much better to long lists of parameters, but *declaring* the constructor would still be unwieldy. The only way to improve that would be for the class to effectively opt into having an initializer using a special syntax for the constructor. Larry gave this example syntax: class Employee { protected int $age; protected string $name; protected ?Employee $boss; public function hoist __construct() { if ($age < 18) throw new ChildLaborException(); } } And Paul M Jones mentioned this version from Hack, where the parameters are listed in the constructor, but don't need to be re-listed outside it: class Employee { public function __construct( protected int $age, protected string $name, protected ?Employee $boss ) { if ($age < 18) throw new ChildLaborException(); } } Either of those, with named parameters, would be almost indistinguishable from object initializers at the call site. Depending on the syntax chosen, it might be as similar as: // Call initializer, requires public properties new Employee { age =3D> 42, name =3D> 'John Smith' }; // Call constructor, requires special constructor definition new Employee( age =3D> 42, name =3D> 'John Smith' ); That would require multiple new features, though, so initializers might be more achievable in the short term, and perhaps there is room for both, particularly if support for getters and setters improves. Regards, --=20 Rowan Tommins [IMSoP] --000000000000f736690592abd5c5--