Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112104 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59745 invoked from network); 22 Oct 2020 19:22:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Oct 2020 19:22:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A72691804C3 for ; Thu, 22 Oct 2020 11:40:19 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 22 Oct 2020 11:40:19 -0700 (PDT) Received: by mail-pg1-f176.google.com with SMTP id j7so1459781pgk.5 for ; Thu, 22 Oct 2020 11:40:19 -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 :content-transfer-encoding; bh=q8BDJszrCccdRcaQPEg8hO0mq+szxF0P4LNBL3kmK5g=; b=R8Vu16/nq/zV1eAPzQxmHFSCSBY+vpnmS5CnqnIV2L8iwdxzP1OFj4g7hT9j8lHDZ9 XeXHC6jU5yFzhod1oQXOsqFkWLLbM7R43EoJGvK/Pfq8x5go3nbha+bEs8GH3CHqnh4q FeADy7K7AL4D1x3iVE5dXTdG3XYAaqnsbE02xthvj5wD2O40vBxGisv+uVnFkvi1gU6D jMYSL9ppj7lKY9t6JCfe6srQFQsmiU33RY2QkIsKniaEuosfpyktiGI28+BhYFJ/k9Wr GKCiRkk//UczvL7Tl1K+0gRILEOGEZSpYFAayNugSfAme6Cbk30luYN5u041oaVNu4VD C3tQ== 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:content-transfer-encoding; bh=q8BDJszrCccdRcaQPEg8hO0mq+szxF0P4LNBL3kmK5g=; b=ByWS6KdYAU+yqPuIFaokaKRLcpeJw9Va/+l7n9FdYsw5bCYv4IhTEGEk7CGAQEVT7n WGEzSTkhlCWr4YJbZYvflOiQsn5j6CmikQwHegec5u9vlhvgCaXPjC8C6hzdygeAVDHo zB9pw6OVq/P6oxmGX9NWnmjNi2A+BgPMaT+QNSLmC7+eiZ+yDvh7YCnUoaMT0EmRxg5Q BkGS5U6OGJW1KAp2+51tDw7RqncvSJK6ZQigSuAtQrPOvR9+HgYWxsax/qbFzTZ0r2ZL +TrbPRfEb2LmXzcNFjpLs3Qb6ZxcOtbJBWSk7GcU3x+tBctks1TxQ3fGaqIM6JRuqL16 JKaQ== X-Gm-Message-State: AOAM532eq1l/GMKc/q3b/CD5GO8nv0OZrxoWmHpNyfSe1/TfiMRwCwRg SixzTAzb08vMcenGKnED0FnCylHgeAITp+J+yNVDGWt3xWeXvg== X-Google-Smtp-Source: ABdhPJwwn5izijbcLywtvJHfNF+uGGR4nbBT+UV5tqlT2Az96k3wO3Uv0KMNJcYM1Rbh1zCYfuka5rPhPXyVd9qkO28= X-Received: by 2002:a63:4d0f:: with SMTP id a15mr3239263pgb.250.1603392017914; Thu, 22 Oct 2020 11:40:17 -0700 (PDT) MIME-Version: 1.0 References: <3e83d882-4281-a5b1-c424-f8b46f92b022@telia.com> In-Reply-To: Date: Thu, 22 Oct 2020 20:40:04 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Is there any interest for object constructor shorthand*just for stdClass* From: divinity76@gmail.com (Hans Henrik Bergan) @Rowan Tommins not saying this is a good idea or anything, but i just want to point out that there's another way to do it in 7.4 $object =3D new class($foo, $foo * 2, $nextBaz) { public int $foo; public int $bar; public int $baz; public function __construct($foo, $bar, $baz) { for($i=3D0;$igetName()); $this->$name =3D $$name; } } }; haha finally found a use-case for variable-variables On Thu, 22 Oct 2020 at 14:40, Bj=C3=B6rn Larsson wrote: > > Den 2020-10-21 kl. 22:45, skrev Rowan Tommins: > > > On 21/10/2020 10:47, Pierre wrote: > >> I'd recommend that if your data is arbitrary, you can use arrays, if y= ou > >> it's not, you probably always should write typed value objects > > > > > > This is pretty much what I was going to say. If you have truly dynamic > > keys, then having the full set of array functions available - e.g. > > array_map, array_filter, all the variations of sort - is almost > > definitely a win. If you know the property names in advance, then > > defining them on a class is going to help you catch a lot of mistakes. > > > We considered arrays once when migrating our PHP 5 codebase to PHP 7, but > the cost benefit analyse didn't show any worth while business aspects. Th= e > only thing we needed to do was to add one line with new stdClass for the > PHP 7 migration to succeed. > > > > On 21/10/2020 10:38, Bj=C3=B6rn Larsson wrote: > >> One typical usecase we have is populating an object with the result > >> from a DB query or using an object to write to a DB. > > > > > > This is certainly common, and even has a built-in mode in PDO. > > However, I suspect that if we didn't have stdClass, we would no longer > > see the need to add it just for these use cases, because they could > > use an anonymous class instead. > > > True, we looked into anonymous classes as another option but it didn't > improve readability, rather the opposite. > > > > > Which brings me back to the feature request: what if rather than > > stdClass, we look for a better syntax for creating and populating > > anonymous class instances? Then you could start with a literal and add > > real object behaviour to it - add methods, implement an interface, > > constrain the type of properties, etc. > > That's a very good point! We have code today that uses stdClass, so it > would probably only come into play for new code since the existing > code runs perfectly fine :-) > > > > I think there are two problems we might want to solve there: > > > > 1. The only way to initialise the object with variables from the outer > > scope is to pass them to a constructor, which is a bit messy (although > > constructor property promotion improves things a bit, see example below= ). > > 2. There's no way to create a truly dynamic class, i.e. one with some > > or all properties determined at run-time, without extensive string > > manipulation and eval(). (Although you can simply write "$object =3D ne= w > > class{}; $object->foo=3D42;" to add a public, untyped property). > > > > The proposed syntax would help with (1), but not really with (2), > > since you need to know at least the _number_ of properties in order to > > write out the literal. > > > > > > Examples, and some probably bad ideas, in the hope they inspire > > someone to better ideas: > > > > // 7.4 > > $object =3D new class($foo, $foo * 2, $nextBaz) { > > public int $foo; > > public int $bar; > > public int $baz; > > public function __construct($foo, $bar, $baz) { > > $this->foo=3D$foo; > > $this->bar=3D$bar; > > $this->baz=3D$baz; > > } > > }; > > > > // 8.0 > > $object =3D new class($foo, $foo * 2, $nextBaz) { > > public function __construct(public int $foo, public int $bar, > > public int $baz) {} > > }; > > > > // Promote properties into named parameters, implicitly defining a > > constructor and passing them to it? > > $object =3D new class(public int foo: $foo, public int bar: $foo * 2, > > public int baz: $nextBaz) {}; > > > > // Extend this to accept an array spread? Means all properties have to > > have the same visibility and type, or default to "public mixed"... > > $data =3D ['foo' =3D> 42, 'bar' =3D> 84, 'baz' =3D> 69]; // dynamic dat= a, e.g. > > a DB result set row > > $object =3D new class(public int ...$data) {}; > > > Interesting alternatives! > > > > Regards, > > > r//Bj=C3=B6rn L > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >