Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107092 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58352 invoked from network); 14 Sep 2019 12:40:48 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 14 Sep 2019 12:40:48 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id CB0B92CE19C for ; Sat, 14 Sep 2019 03:17:09 -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=-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: No Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) (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 ; Sat, 14 Sep 2019 03:17:08 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id g13so36690446qtj.4 for ; Sat, 14 Sep 2019 03:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=beberlei-de.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uxLhd1GSMymcxiuEfQE63nOVqoU+5THvs8GGNlY0QYI=; b=TC0Q8lGqMuHwU5zchze3hIfwrXXtR+Koa9H7A91s0V1pjjNGukgCyuetBBy6lZvaN6 ZVY77ndAdx1QPKabyAfOAn+LQopzdFeQbyQHndMAjz6heP2CiWvRntfRQG3NDyxQIPEQ Nzyf+hEaUqOJyQuwBVs0RmDJPhTxiW+8IBHPtqrSgLi8FDH5CJh+Zc5TuvCtERnEmIu2 kO/HbbRM/KVlkIT6DL6sd7pDh0KMs9tLHMvjDsVTuhjBOtwO9SecY0mvGT0ZJJ++7Rew pppS69epZINfCWE6X6pyD7ZCE9Uw0g5jHHd2ejj1K9XnlKmOn2muudOHlCeNjRkrYP5e Njhg== 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:cc; bh=uxLhd1GSMymcxiuEfQE63nOVqoU+5THvs8GGNlY0QYI=; b=aI1zf6+j++srBQPhegYsLg1K1YYVygHyaeP9ZJexOyXIipi69GtEL2WPKhY029BxBD RUFLMUhMCw5qZR0V6yk9VZeo7keatIxlAjE5dF3H4K653lApbtdA58yR3PAs30mI+KmI u6MbZFCm58ClObDo2yaXc4dpK803qJsUqN+uP5wEWIivq8SjbB/TQp84NgDeLfenRJEW 283rQd3/welrhxJ6cV3BK8H045HSeUkfgBk9O8wRSuIJ8LH2i/OeXmvlBiqOk1uHV0mk qZNwOSB+755kjdD+sOlff5H8z2mYYHEeqoyQb9QqGd3TkoK+3u1nW61L5cJEgLSrMQr9 WiwQ== X-Gm-Message-State: APjAAAW8+3DHDBJccRHgl+ra9xcksb/hGGcsO4j6klRngc9cmZBvIotq CRMEgbV/fqZHe1cC/esj8GgCm7SYl5A+udLMjgnUKQ== X-Google-Smtp-Source: APXvYqyasoMQCIT4e8c7Zpn925bV6x9olkoxJPE4V4zfVWcscQXiyj5wGZ7zMHRvrqo9nA3YT0sFFHIcYSZCtGFqy1Q= X-Received: by 2002:ac8:524f:: with SMTP id y15mr7827386qtn.354.1568456228333; Sat, 14 Sep 2019 03:17:08 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 14 Sep 2019 12:16:56 +0200 Message-ID: To: =?UTF-8?Q?Micha=C5=82_Brzuchalski?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary="00000000000055fb9a059280acc7" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: kontakt@beberlei.de (Benjamin Eberlei) --00000000000055fb9a059280acc7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Michal, thank you for this RFC. In combination with typed properties an object initializer syntax becomes highly thought after in my opinion, especially if you consider classes consisting mostly of typed PUBLIC properties, for example data transfer objects or view models. I find this especially important, because it helps with definitions that otherwise create unitialized variables: class Customer { public DateTime $created; } $foo =3D new Customer(); // $foo->created now unitialized Using the object initializer would leave the object in a fully consistent state: $foo =3D new Customer{ created=3Dnew DateTime('now') } Two points I want to mention: 1. Currently with typed properties RFC its possible to declare a public property as not nullable, but don't assign it. PHP will make it use the new "unitialized" state. This is also done to allow unset() properties later to allow hooks via __get. But with this new syntax, we could enforce that if a class is new'ed with the object initialzer, then at end of the object initializer + constructor, all properties are initialized, otherwise Exception. This could help with bugs where you add a property to the class, but forget to assign it in all cases where the class is new'ed + intialized. One problem could be with RuntimeException that this only crashes when the code is run, which is often too late, and would rather warrant to just proceed and assume the developer knows what they are doing. Also since object initialiizer only sets public properties, the check should be for all public variables are initialized. 2. You should add mention how the Reflection API changes. I would assume both ReflectionClass and ReflectionObject get a new method newInstanceFields(array $fields) with the following behavior: $reflectionClass =3D new ReflectionClass(Customer::class); $customer =3D $reflectionClass->newInstanceFields(['name' =3D> 'Bert']); // the same as $customer =3D new Customer {name =3D 'Bert'}; On Thu, Sep 12, 2019 at 4:01 PM Micha=C5=82 Brzuchalski < michal.brzuchalski@gmail.com> wrote: > Hi internals, > > I'd like to open discussion about RFC: Object Initializer. > > This proposal reduces boilerplate of object instantiation and properties > initialization in case of classes without required constructor arguments = as > a single expression with initializer block. > > https://wiki.php.net/rfc/object-initializer > > I appreciate any feedback you all can provide. > > Thanks, > -- > Micha=C5=82 Brzuchalski > brzuchal@php.net > --00000000000055fb9a059280acc7--