Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116017 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 54076 invoked from network); 8 Sep 2021 20:21:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2021 20:21:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A522A1804DB for ; Wed, 8 Sep 2021 13:59:42 -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=-0.7 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS 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-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 13:59:42 -0700 (PDT) Received: by mail-wm1-f52.google.com with SMTP id n14-20020a05600c3b8e00b002f8bd2f8ab6so2586653wms.5 for ; Wed, 08 Sep 2021 13:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=oXZA7cl4giZrlUNMfUaI2w8NOvvyHF5ARPAzCxZmGpc=; b=O8cC7iFYqwVhlC2fiadHqp8XHhDVeHUN5Ybj41D0TSWsG7UbvB11EoouDO0Bu8aC1c GbbFxLpwA0HKT7TrugvqWvFB6G0MiuE9D7CUVbcQXx+nDQAv14V372HZiAE8AjUtXwL0 DZlsH2OPYpZVka5DU3/zFsdz+C53zY5gUbqonTolmHWx53KzYdiPLkZLORUfD13SAjIC vZZhZ828k3UigY3ne+8gRUDiNVGVIRUNa+lb6pd2b0J7avnijM7E9N3UTNilFFxk9NSj AAP216jxjiXMi2LObaqqo7rgEB7iLhz9kI4mG/Jnf8Y8S9ZbQJxf5oM8oaX6bHkxhKBy E6Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=oXZA7cl4giZrlUNMfUaI2w8NOvvyHF5ARPAzCxZmGpc=; b=CEajMc3+ZjoTeIG3oTm6ybmqzBKJ56lCcH+4aZ9WWg2fDn//XzcL0ZzA7OMv2yygUz aIRPmEwXelC6By8/y0R/inN8vcs2G3GaMxKviO9L1ucgCw1BIrRG6uXdTBgoxj970kfu 5av0JbhAnbCFOv3Ik6yWgV2EgZYH2pHd8/ziF4K6YJ2/uo4vMbixDzBxEtwiOKO/aSxx WHL9ZiAbGe2zABtDBd4s8ftQlK74+Z7/Rbr/Zs1Q+4KLAJIJgd6gmNaDiVZaQ9H+xERS Mi7ADjPLskVjcQizBOS+/qtRZeqp2yXDSIVHg9aJ7h305xBQaAgczUmvC/502+tgXApe z7qg== X-Gm-Message-State: AOAM533zEeyCbB1s3p9ExFAk6tpNeAW4x7WUoyOnKCagUwA7RFSK7Xkl ltodoEX+VkHk0HeIvOj6JexDYHs7h7M= X-Google-Smtp-Source: ABdhPJzbW0iyb4v3IPpai7SmJmQXYG0cEAYtR+qncrJX7Xn1ElHALNb39Ww1gGRG2OsFFmBw2XOHtQ== X-Received: by 2002:a05:600c:4f53:: with SMTP id m19mr5443503wmq.2.1631134780072; Wed, 08 Sep 2021 13:59:40 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id g3sm197627wrh.94.2021.09.08.13.59.39 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 08 Sep 2021 13:59:39 -0700 (PDT) To: internals@lists.php.net References: <46f2e842-8608-0622-f1c1-3eb746658c84@gmail.com> Message-ID: <0f084050-ede2-2c7a-90f0-26e17fe94836@gmail.com> Date: Wed, 8 Sep 2021 21:59:38 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Alias stdClass to DynamicObject? From: rowan.collins@gmail.com (Rowan Tommins) 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. Regards, -- Rowan Tommins [IMSoP]