Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112111 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55876 invoked from network); 23 Oct 2020 20:57:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Oct 2020 20:57:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B000C1804AA for ; Fri, 23 Oct 2020 13:14:54 -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,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) (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 ; Fri, 23 Oct 2020 13:14:54 -0700 (PDT) Received: by mail-qv1-f51.google.com with SMTP id s1so1360271qvm.13 for ; Fri, 23 Oct 2020 13:14:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=aIFpSHcRAXdvEI3ZbTVwT/ofqxxBH7edK2BLOuHXW+I=; b=oyKEKGdpRe/fgwhi/cFtOna1/+lrn4EFQ6fDuavwfCxKXz0HFhAfYx+WZb2tsZvx21 l+SCqMZN+9wM1bP03ruYSkpZN66/ZVxrzWa5UeMvuijmixTo8RbA8xjL2OiuWmzPx4Us BqslA3UGN+v8q1+OaS+fRMnpA+MvMD11L9Gsu3Siv1tdVHfTT33mK4FYWkuuQ1oYk13e lxIf81wqJiQjJRwp+oQiIa/8WV2mipyRw4Q/ZYvZUvtSWsWqmQL8cNCXR2wJoVxuzABy oE4IBhjfhF3K4SSaUrg02HwQIFv15j+GgJkrFzoHhlhMjuil1v860nV2c6MOIcV4M2Gk VU9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=aIFpSHcRAXdvEI3ZbTVwT/ofqxxBH7edK2BLOuHXW+I=; b=Rhdi2YUd0SAjmCKwvSjpxT8f9ABbxaGvcCtLsaowDD0yC3cGTlBETjfwOVtUE3MIC3 WVALhl27vkLlvx/m/zSs/pf5CJ527D0vrz/WSkV7rfq3/Zh6kMWr5WfgOgdA41IunzTB r7dT7g1PkX/CH4I5BABZFc1hGYay46s9Wm+XXnvtgtal4yJrdbWsAxmUaZuwFwlOKzA6 D3y9Q9p0a7/WVvM78Rh19i6dBslgzkHbQwmCkeSnPDY8CgyRXBxRC3NbbbpyC+SWFeaL ptesvh1FmjRPQu2a6qbGMLydnbfkXivSrjOz7P1pFZsJsBYwfMF9EoL8xz5/BolNKC9O kuhg== X-Gm-Message-State: AOAM5324crISr7HN0SdNIzCO8NwwEMOrLaUToE5kv4ecJM8dAGlusoEw XAHgfe0V0Z/F4VoOzMUYc4e95Q== X-Google-Smtp-Source: ABdhPJw8nGJbC2lQOli7JQGOxotefMoeU8cz71yIVMjx8Zm76q7Brib1r+l4SJGzbx4RSUTxon8TGQ== X-Received: by 2002:a0c:b410:: with SMTP id u16mr622444qve.52.1603484091673; Fri, 23 Oct 2020 13:14:51 -0700 (PDT) Received: from [192.168.1.254] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id 19sm1471643qki.33.2020.10.23.13.14.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Oct 2020 13:14:50 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: Date: Fri, 23 Oct 2020 16:14:49 -0400 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <2A808AB8-7179-41F6-88F4-A19C8F7FA171@newclarity.net> References: <5f92f1e0.1c69fb81.a800f.cbe1SMTPIN_ADDED_MISSING@mx.google.com> To: Rowan Tommins , Andreas Bittner X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] Is there any interest for object constructor shorthand *just forstdClass* From: mike@newclarity.net (Mike Schinkel) > On Oct 23, 2020, at 2:33 PM, Rowan Tommins = wrote: >=20 > I don't follow; is the resulting JSON different if you cast to object, = or is there some other reason you prefer an object over using an = associative array directly? The by-reference semantics of objects vs arrays in PHP. > Also, to clarify my earlier comment about stdClass not being necessary = now we have anonymous classes, I meant it very directly: every time you = write "new stdClass" you can write "new class {}" instead. >=20 > The fact that "(object)$foo" creates an instance of "stdClass" rather = than an instance of "class{}" is just a historical wart, which can be = easily replaced: >=20 > function array_to_object($arr): object { > $obj =3D new class {}; > foreach ( $arr as $key =3D> $value ) { > $obj->{$key} =3D $value; > } > return $obj; > } That pattern can have a non-insignificant performance penalty when = dealing with a large number of objects, a use-case that is not = infrequent when processing JSON, especially responses returned via an = HTTP API. > On Oct 23, 2020, at 11:08 AM, Andreas Bittner = wrote: >=20 > Wouldn't it be opportune to just use named arguments? >=20 > $x =3D new \stdClass( > prop: new \stdClass( > a: $a, > b: $b > ) > ); You illustrate of useful pattern here.=20 What would be nicer, however, would be an object instantiation shorthand = that would omit the "new" by using braces: $x =3D \stdClass{ prop: \stdClass{ a: $a, b: $b, }, } Also: $x =3D \Foo{ prop: \Bar{ a: $a, b: $b, }, } And for instantiating anonymous classes: $x =3D { prop: { a: $a, b: $b, }, } -Mike