Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107045 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 12391 invoked from network); 13 Sep 2019 09:39:36 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 09:39:36 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 44A532C2BB4 for ; Fri, 13 Sep 2019 00:15:41 -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-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) (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 ; Fri, 13 Sep 2019 00:15:40 -0700 (PDT) Received: by mail-ot1-x334.google.com with SMTP id n7so28528384otk.6 for ; Fri, 13 Sep 2019 00:15:40 -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=gCgxsNX7JTTMosaFiIMMKegE9glqF2gUIS3IHwiC6F4=; b=BchfmH/PxEK90simwM6AEZeKJt/F860DIEb2gPsF6SfAEwRbzBcJRN/EXwxjed8aSj CD18x48ntngwyQ0ts7sfgBFRBoTkgAJTiLkfHIiboX2XijZaWvDs4jbX3UlG1IAgA8vV BWQV5OToO8A7ZQES/ADZD3gDWibNsi9nfv8KDaDLxJdVtaq+lz37bk1ta4Uic4alY6AV 4L2lsb6yYmlZLzpHeCzqwZTVShcp2d5wu9XwGsLdswcqpzhYemDlJjcItgycRQC7DAyt oaO97iDTQRBr5dN0+Y/lUQVCLWQdZFSJWgvs9unQdoJhLM3z6mgBCRHgQ4i2MLERSUjs 4B+A== 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=gCgxsNX7JTTMosaFiIMMKegE9glqF2gUIS3IHwiC6F4=; b=H441h52bVQNiPyb32nc48oWfIBg+IhMsc0Xs2+7+TzpYVDTcRbwUnJPT3Kj8slqb4J XzI3UPhPaGyYV6uukmGYeQT86X/e1ehJ6IMSSIzC74JLeDQzt3oygVfcOFMxgcmmbZeY qHeDvFYfNggIdHljTGvHNLpHlhJDxoRfDOpXkBPucYzArbIL3MsH16sXBCbV3qwBmmmN YO2sULtJUwLMeQ7JwlY0S/GW9V6uE4a43Swf5VHRUaBjBeDgD/IQv6CG5ifVuF+/9eSc TClU3LvQUeoiXLnPQTkKwfdmftOKRnCemq/qWB+IsbnT4K9llsXWzv0CBlOaUIghr2CM 4aHQ== X-Gm-Message-State: APjAAAXh+uKv7bHR64QbUO3Pwzkh30QnJ8xXQhHJTNURn4s1nPFFi5pq HyXOSoCvfo9Zk4Von+h2Rm68ZGlLWIwaIRngCWs= X-Google-Smtp-Source: APXvYqx8ysv9OJobSfL2t1cHbUD1LP9lWULy/wxKjp6SCeXmkuxY46wXe5xFmRDUBHDyMYqeX07emPFMWRJev1SHy1c= X-Received: by 2002:a05:6830:1d85:: with SMTP id y5mr21800150oti.214.1568358939973; Fri, 13 Sep 2019 00:15:39 -0700 (PDT) MIME-Version: 1.0 References: <74C03BB1-28DF-4694-A91D-2516C545F8B2@gmail.com> In-Reply-To: <74C03BB1-28DF-4694-A91D-2516C545F8B2@gmail.com> Date: Fri, 13 Sep 2019 09:15:27 +0200 Message-ID: To: Claude Pache Cc: Lynn , PHP Internals List Content-Type: multipart/alternative; boundary="0000000000007f5e6905926a0598" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --0000000000007f5e6905926a0598 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Claude, pt., 13 wrz 2019 o 08:39 Claude Pache napisa=C5=82= (a): > > > Le 13 sept. 2019 =C3=A0 07:49, Micha=C5=82 Brzuchalski > a =C3=A9crit : > > Hi Lynn, > > czw., 12 wrz 2019 o 17:01 Lynn napisa=C5=82(a): > > Heya, > > What's the added benefit of this compared to implementing a constructor? > > The part I like is that this can be used to replace stdClass/structured > arrays. Perhaps something like this would nice to have in PHP: > > ``` > $people =3D []; > > foreach ($peopleFromDatabase as [$id, $username, $name]) { > $people[] =3D { > Uuid id =3D> $id, > string username =3D> $username, > string name =3D> $name, > }; > // and possible automatic assignment: > $people[] =3D {Uuid $id, string $username, string $name}; > } > ``` > > > Removing stdClass for instantiation and initialization of simple objects = is > one of a future scope proposal. > > This RFC tries to address instantiation and initialization boilerplate > reduction with a syntax which would > not be restricted to stdClass only. > > Although it's not a game-changer, simple addition to the language which > reduces boilerplate when dealing > with objects which don't need complex constructors like for eg. DTO > objects. > > > As for `stdClass`, PHP has already a syntax: > > $baz =3D "baz"; > $obj =3D (object) [ > "foo" =3D> "bar", > $baz =3D> true > ]; > > For other type of objects, that could be done with a simple helper functi= on > > $customer =3D object_assign(new Customer, [ > "id" =3D> 123, > "name" =3D> "John Doe", > ]); > > where: > > function object_assign(object $obj, iterable $data): object { > foreach ($data as $key =3D> $value) { > $obj->$key =3D $value; > } > return $obj; > } > > That said, I generally use arrays rather than DTO objects or such, so tha= t > I can=E2=80=99t speak from experience. > > =E2=80=94Claude > > You're of course right this can be done more or less with a helper function which does all the required type validation through reflection, but I believe that feature requests for language support which reduces boilerplate can easily spread that simplification and bring all the required type check validations out of the box, right? IMO the thing is as you said this would require a helper function and not all simply like them! Regards, Micha=C5=82 Brzuchalski --0000000000007f5e6905926a0598--