Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116018 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55805 invoked from network); 8 Sep 2021 20:34:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2021 20:34:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B2C661804AD for ; Wed, 8 Sep 2021 14:12:43 -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_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (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 ; Wed, 8 Sep 2021 14:12:43 -0700 (PDT) Received: by mail-pg1-f169.google.com with SMTP id u18so3988143pgf.0 for ; Wed, 08 Sep 2021 14:12:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=RytBVXa0SPtI880iBxqL/bs48uHcqM5j/CC1R9VpoUQ=; b=VSk8XYmPYMEUAAu39ZqTJKt0Phv1oU3QX5Dc0djG/vUIlUYPcANEjR8jajxsQ/vRaN Q7ggeEH+th+7eCdqa+90PcBqZfSWFyulJlz5Oy1r0xRq62Lro8tKrmRbCiUQJpSCPvmL yC6S4kJj6y9E38pKB8nlIc/9zIIv2uMmX5nSTx8WjBgz/ixh7H6nJklaAk0hYBd47/KN 0ExU6wxX3hXjWDUhvHP0CzlG376QOnVT/hXPTa8n79gDirN9dIKFa4qDiZ04PJ82LmEv V708jPEAOTsnBIvU/rHRaadgOTEVBebgPA6xQ8SUBZKHGzUF8WNDsbJfIiOUrk6ceYn/ IbQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RytBVXa0SPtI880iBxqL/bs48uHcqM5j/CC1R9VpoUQ=; b=Cha0CSmB+LR3Wi73g4E9SE6NLnJNafasqoHASMp69iakNfW4L3KI5s6Mqx3pWzJFcV arDcFYxxqTmxAdIv+hcbT+g4JzLi5int4ybyPB+l1/4DRjYPaJMhJNW3VFlNdZ2hXwYL vXWEJOgMsUNOQtkeybkSWmOwTEMh6/JNDYtOu0YoPJanAm2oM/2jAlmoZaziB5W4bdKE FAzbDaWuRl3KCQ0taW7rJsxfcLbg6cmKAoqgEq6B3vJeQaufeBAnONVdffh5tEXckniI YcSuum8Fypcljddi/MjBvhhgb4rrRF2bL+h2Rg2M7cJMQudiSJ4hN6531MjXfQUKMu87 MvXA== X-Gm-Message-State: AOAM533oOZ4vpYHbVSbKhalcLZuj5yvmfYVgcwUuyllQeYdI9bDYm5XT S3pyKcrLQyn1ce46v+713ATjzLRZLvbafYZIKBWx/g== X-Google-Smtp-Source: ABdhPJwiT+1z1kq9LU2ATJ+6nqVKh/DSRYj70i5q2ehenUanSeE5Zd1SOTuoef3WgY7xTSPoJdJe90vh3eLl5ndze+8= X-Received: by 2002:a63:d10b:: with SMTP id k11mr168149pgg.26.1631135559023; Wed, 08 Sep 2021 14:12:39 -0700 (PDT) MIME-Version: 1.0 References: <46f2e842-8608-0622-f1c1-3eb746658c84@gmail.com> <0f084050-ede2-2c7a-90f0-26e17fe94836@gmail.com> In-Reply-To: <0f084050-ede2-2c7a-90f0-26e17fe94836@gmail.com> Date: Wed, 8 Sep 2021 23:12:28 +0200 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Alias stdClass to DynamicObject? From: andreas@dqxtech.net (Andreas Hennings) On Wed, 8 Sept 2021 at 23:00, Rowan Tommins wrote: > > On 08/09/2021 16:37, Mike Schinkel wrote: > > > All future code that needs to refer to the class name will still refer to `stdClass`, so we won't be gaining much by creating an alias. > > > Just to be clear, the only code that would need to change is code that > dynamically gets *out* the class name, from get_class(), var_export(), > reflection, and the like. Using the class name in code, like "$foo = new > stdClass;" and "$foo instanceof stdClass", would carry on working just > fine, whichever way we defined the alias. > > > > Besides, don't forget `stdClass::class` in addition to `get_class()`. > > > The ::class syntax is purely string replacement (apart from some rare > edge cases), and works identically whether the class exists, is an > alias, or doesn't exist at all. > > > > I assume we would also disallow dynamic properties in anonymous classes too, right? After all, they are just statically declared classes that the developer do not assign a name. > > > The difference I see is that stdClass/DynamicObject allows you to add or > remove properties from an object *after it has been created*. I think a > lot of use cases don't actually need that, and would benefit from error > messages when doing so accidentally. > > You mentioned short-hand syntaxes like this: > > > $obj = { > > foo: 1, > > bar: "hello", > > baz: true, > > }; > > I would love for that, or some other short-hand, to be equivalent to this: > > $obj = new class(foo: 1, bar: "hello", baz: true) { > public $foo; > public $bar; > public $baz; > public function __construct($foo, $bar, $baz) { > $this->foo = $foo; > $this->bar = $bar; > $this->baz = $baz; > } > } > > That is, an anonymous class, with exactly those three properties. If you > *also* want to be able to define extra properties after it's created, > you could opt into that using whatever mechanism a named class would > (parent class, trait, attribute, etc; see other thread). > > Similarly, the objects created by json_decode or PDO_FETCH_OBJECT only > need the *initial* properties to be dynamic, not to allow properties to > be added later. Even if no properties can be added after construction, this would still mean that the list of properties can be determined at run-time. There are two ways this can work: 1. Every new instance has its own anonymous class, even if they were created by the same statement in code. 2. Different instances created by the same statement in code have the same anonymous class, but this class supports dynamic properties. Btw now that I think of it, I have seen lots of code where objects from PDO are modified post construction, also with new properties being added. > > > Regards, > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >