Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107043 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 3848 invoked from network); 13 Sep 2019 09:03:50 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 09:03:50 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id CCBC22C0D85 for ; Thu, 12 Sep 2019 23:39:55 -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.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,FREEMAIL_REPLY,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: X-Spam-Virus: No Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) (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:39:55 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id i8so26007781edn.13 for ; Thu, 12 Sep 2019 23:39:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=3DMGI6A6/66wwZpN/pXWcU6L+FE7vA1Lf1FcG8IlkWk=; b=bBmgbaXgfadRyyEAzbOOb5/NSAHOpJFSMMNV28vvdj2LJWTHvvL7c2fC6zP+Th/yAu flJvmOxt172X5DqeGqbdX8dnByGWfGuSByEAW/W3zfHIdqBCcXY7JODDx3PgxQrNGO4b 8cfYf/EhbHxzZ2Wo9QF+dfNtQaX81Z/lUW3/Sh5A1E3o+52CTbjwIMWWlRnf0f1OOOfv KbDOJACKobHUySuBuK4lG1LN9YYy3kcvipd8b/X6V7jDFr8mgm7kWc3G5joqTTBWj+i4 UsNdHl/rwX9wcEiKxMvamANBaDPfd2OGMrs1/nkE31XMdAUpXxjXP5GUANw8T89n2S/J p6kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=3DMGI6A6/66wwZpN/pXWcU6L+FE7vA1Lf1FcG8IlkWk=; b=PK8T4vEeZUNLUmRw7rL0yXn6xKQWryUhpDwzc62Ya8oUhzFfdReQplg9C0AcsnRxjE 7XNxVOX4mWcxkC32egWuxT6PoGdpRhXxLWlp9QWvF+eK83G0gxuECMwzD5XOHmYYu0CH nStXyHCI4DqnMHezK0ylmSHxOBtOcC9ocAYWAcHC6xoV1MzFsfACGuRalCgdGSH0ojZY c3iVEgioXPeFH/PkRPRtuSNDvh0xGAYhm465+rczyfRCxjTakfQI5axIYias2K33fUte 46fdmR7xbXyHzAZ30iXwLnUQitBJaf89BCe30uLJ/M5EDxtfLOe4Q0Eo4v2pKn/qtIjL inWQ== X-Gm-Message-State: APjAAAUBPbvgh7ihFswwMH4A5Q5j5WfM4mhur/ATTr+gFVgutq5fA0vo xIl9hBLuCPvKq7udj+Slmt8= X-Google-Smtp-Source: APXvYqzoD0+vLSFbkcje41R0B1IC29WAGWiZD+jkX5jAuvzNCnL9RUBBEOmC2Y3uNfmtLPkNooCyNw== X-Received: by 2002:a50:b885:: with SMTP id l5mr44622725ede.190.1568356793993; Thu, 12 Sep 2019 23:39:53 -0700 (PDT) Received: from [192.168.0.63] (84-75-30-51.dclient.hispeed.ch. [84.75.30.51]) by smtp.gmail.com with ESMTPSA id ay23sm946539ejb.54.2019.09.12.23.39.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Sep 2019 23:39:53 -0700 (PDT) Message-ID: <74C03BB1-28DF-4694-A91D-2516C545F8B2@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_72468D4E-CB8F-4A07-A3F2-400AE9D85995" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Fri, 13 Sep 2019 08:39:52 +0200 In-Reply-To: Cc: Lynn , PHP Internals List To: =?utf-8?Q?Micha=C5=82_Brzuchalski?= References: X-Mailer: Apple Mail (2.3445.104.11) X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Object Initializer From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_72468D4E-CB8F-4A07-A3F2-400AE9D85995 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 13 sept. 2019 =C3=A0 07:49, Micha=C5=82 Brzuchalski = a =C3=A9crit : >=20 > Hi Lynn, >=20 > czw., 12 wrz 2019 o 17:01 Lynn > napisa=C5=82(a): >=20 >> Heya, >>=20 >> What's the added benefit of this compared to implementing a = constructor? >>=20 >> 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: >>=20 >> ``` >> $people =3D []; >>=20 >> 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}; >> } >> ``` >>=20 >=20 > Removing stdClass for instantiation and initialization of simple = objects is > one of a future scope proposal. >=20 > This RFC tries to address instantiation and initialization boilerplate > reduction with a syntax which would > not be restricted to stdClass only. >=20 > 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. >=20 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 = function $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 = that I can=E2=80=99t speak from experience. =E2=80=94Claude --Apple-Mail=_72468D4E-CB8F-4A07-A3F2-400AE9D85995--