Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107042 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99032 invoked from network); 13 Sep 2019 08:42:29 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 08:42:29 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 16D332D2007 for ; Thu, 12 Sep 2019 23:18:33 -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-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) (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 ; Thu, 12 Sep 2019 23:18:32 -0700 (PDT) Received: by mail-ot1-x32d.google.com with SMTP id z26so19167700oto.1 for ; Thu, 12 Sep 2019 23:18:32 -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 :cc; bh=k5HwstYqoTbT4tJUSy6SdoYz9TBBRrNlkOPrY0Igf0g=; b=qHqc+gxyRVT5xNAud7wwBE0NwpOrxWcXGzqr50hVslLkY969QH1n4BrFBtXhxS2TfM Di3YnggYf2iQIGJd5dKeA/yw9M3Y4ICKZRwfyaUWhGUxXLzSygBu+H4kMf1l0318jmuA QDFI+/2JJ0TfXh7oqBpmdtrGF1SiMvaV9PEgdHwzl8wxLtD5Nt7Q1FZDq4oZi11nkd36 MvpFiZ9J39cvssAqRKalzDq/Ifx8b1GdfI8YlzrL/S5BBMhqMuUyNGS4w6grrZXMdkrg F+hY8YYzvf/ezxuu5MUb3LgSziFsu+h0soSSBCTSeYdbVl95TqDbSYNsQp8f4V2c0bLU ybww== 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=k5HwstYqoTbT4tJUSy6SdoYz9TBBRrNlkOPrY0Igf0g=; b=fu7/XS0X/5CCPRoE6Mopbme0Y77PgZeD5NXUQDz4g8yaxEa0Q53u3NPMPCDPdGWfA5 OQGWMvnryh4o1oHGhmNNn+3U9hJxiQF5tznyktx7WaSP2yi7jU+a0+nDbnWv6B3pyg7a zZA/sI4Tm6QW9CPnQlinCB0GxpJgi4Kbtbf9PmNI/Mw3k6njIT4psEJ++040GPIMdOed 7xfn3VuAzwGhsRK2t/Tcm8mRiRiGN65bxGJ8lWsCeg2h6SsJB/XukJIXDsmJXnhYMSCW 2nbCgvtQgZx8083fFvmfGmxj55RgSM2t7+XxZQ1P2H6RRiNz7ajuG5l+szPjyojaIHew RMtw== X-Gm-Message-State: APjAAAUBdpC0EnS9Zcv5eDVwZVm5AP/VJS+M7zP+Vzr61ApJaNkm0QXB sUAxISFdoO8nKd6xsa7ydk7fbAwY4oQ/IbDND4Y= X-Google-Smtp-Source: APXvYqylTFxH9ZmqudJR1cU+psTbGh8YNezxScoDFWfoMjWV9zp0rx0cQL520gbtjUGHR9ZcfrwJIKribEurte+Hsn8= X-Received: by 2002:a05:6830:1d85:: with SMTP id y5mr21676393oti.214.1568355511623; Thu, 12 Sep 2019 23:18:31 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 13 Sep 2019 08:18:16 +0200 Message-ID: To: Olumide Samson Cc: Lynn , PHP internals Content-Type: multipart/alternative; boundary="00000000000026e3ea05926939ae" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --00000000000026e3ea05926939ae Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Olumide, czw., 12 wrz 2019 o 17:07 Olumide Samson napisa=C5= =82(a): > The RFC is a beautiful feature suggestion, but something is telling me as > beautiful and straightforward the syntax is, what can the use case be? > > I really love Constructor or Object initialization to be implemented in > PHP, but is there something I think you're missing in the RFC? > > Thanks for the RFC. > I'm open to suggestions. PHP is known for borrowing features from other languages and this feature is also known from other languages. * Rust allows whole struct initialization in one expression, although these are not classes but being able to instantiate, initialize and return instance directly after it's being initialized reduces a lot of boilerplate. * C# uses an object initializer for the same purposes, one expression which allows to instantiate, initialize and use the object directly after initialization. * Java users use hacks to do the same, even more, cause they're allowed to call logic through method calls inside "initializer" block. In some cases when you deal a lot with DTO objects, object instantiation and properties initialization is very verbose and introduces a lot of noise= . class CustomerDTO { public Uuid $id; public string $name; public Email $email; public Address $address; public City $city; public Country $country; public PhoneNumber $phoneNumber; public etc... } Given above DTO class instantiation and initialization now requires: * INSTANTIATE class and assign to a variable: $obj =3D new CustomerDTO(); * FOR EACH (can be many) needed property use the variable with object instance and assign the property value: $obj->property =3D $value; Now when you deal a lot with objects like that you can see you're constantly repeating yourself with $obj-> part all the lines down before you probably would use that instance. There is no need for the constructor in such objects, they're purpose is to transfer data between app layers. Last months I've been working on a project which often requires to create an entity with data decoded from JSON format, most of them had many fields|properties required and only some of them were optional (even those optional were nullable so possibly I could initialize them with null once again), so the case with which I had a lot to do was creating factories which instantiate and for each property initialize value just to be able to return newly created instance in the end. That's when I thought object initializer could reduce a lot of boilerplate. That's when I started thinking of many places where I could use that, where my IDE could help me to write code faster without all that noise of constantly repeating myself. The syntax is similar to other languages. The narrow case described in BC changes is already deprecated and in my opinion, probably even not used or very rare. The future scope features can potentially save even more strikes but that's not the main reason about that RFC - nice to have but let's start with something. Regards, Micha=C5=82 Brzuchalski --00000000000026e3ea05926939ae--