Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116209 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94519 invoked from network); 4 Oct 2021 08:26:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Oct 2021 08:26:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 603A51804DB for ; Mon, 4 Oct 2021 02:10:20 -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_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, 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-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 ; Mon, 4 Oct 2021 02:10:19 -0700 (PDT) Received: by mail-ed1-f54.google.com with SMTP id v18so61118188edc.11 for ; Mon, 04 Oct 2021 02:10:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cO+I6q17qanVz3WU8cxqAShjsTRcqiaPqzPutfLeMgQ=; b=H/6eocxGx2sfVpNPFgDH5dyaRcT0BTogTgMjBOSnD0QKxftvthGcA4xz9zGm2l9QrS OIh6GbtqCwBnwYSXJc/k5Y9wsaBpVggaIYQYvQ3TLSFnZu0KIwdTUWUe4ncyp6iFGg6x bSbyFMc1q0CTAv8O/nKqf9BIbKRrd6DFd+BqqhkEhi2MGWO/kBDGOkgzE1clV7WWo2tK ZOLMHSTTKlYtif24b4t8uTROcoj13MpanXoDxyyTHCCGxcuFMDONtoe+18lWT91p/p3g iiUJPDIcEMWm2zZtVSRuOyYqKnb+a1PBuh36fmqslTnD8Mnnigj3Gitd/7HbTlyRqoQK mztg== 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=cO+I6q17qanVz3WU8cxqAShjsTRcqiaPqzPutfLeMgQ=; b=l57UOQCsIFmf1GQeek/QWawjmuDirjS9pr5vBz2cnr2jbcxpgy2ajbb4EM5eRXX+9s zFAjyq1ljPz/HviBXGec3Ax+ms6dm1tGpmgKZOA2lTo8EMJouYikxGOzLY1WoPwTqGyH /lCrjAYX7jaY2qD6V2auTB/OnULmW+1HQcaswZq4EupccuDTNHXzs3/h/oD8ivY1Tqhu 8YlGUNx/Q3FzOogDUtju0CURWdDCuuZkBE4UcEChtFtbsfjJj8xQmjJQDMh1qUpJZkYQ ioWsBnBa35SUvm/9A9RcolGfKwt+aJuGO9c0NsRB7JXUuoQ1lqPotbOuU5bBkNObzF5P wcTQ== X-Gm-Message-State: AOAM532Z6dLVWBTW8qhe4WEQjrCCYuRAATRQwKtFiS+ajiopJXd91sds FX0/nXTF2pqzyp9kpmEwIMjk6FrnOFDBtjhCotQ= X-Google-Smtp-Source: ABdhPJy/am3OIaUl9BlbbxoJzfyU5c77OSmmi8HKpHv54O1ejnWNNwq6RVIoONLNhIws5x8sOhvZKXhfRyRgYBudpBk= X-Received: by 2002:a17:906:3882:: with SMTP id q2mr16289725ejd.396.1633338614951; Mon, 04 Oct 2021 02:10:14 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 4 Oct 2021 11:09:59 +0200 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000f1337105cd834708" Subject: Re: [PHP-DEV] Allowing `(object)['key' => 'value']` in initializers? From: nikita.ppv@gmail.com (Nikita Popov) --000000000000f1337105cd834708 Content-Type: text/plain; charset="UTF-8" On Sat, Sep 25, 2021 at 5:45 PM tyson andre wrote: > > Hi internals, > > In PHP 8.1, it is possible to allow constructing any class name in an > initializer, after the approval of > https://wiki.php.net/rfc/new_in_initializers > > ``` > php > static $x1 = new ArrayObject(['key' => 'value']); > php > static $x2 = new stdClass(); > php > static $x3 = (object)['key' => 'value']; > > Fatal error: Constant expression contains invalid operations in php shell > code on line 1 > ``` > > What are your thoughts on allowing the `(object)` cast in initializer > types where `new` was already allowed, but only when followed by an array > literal node. (e.g. continue to forbid `(object)SOME_CONSTANT`) (see > https://wiki.php.net/rfc/new_in_initializers) > > stdClass has never implemented a factory method such as `__set_state` > (which is not yet allowed). Instead, `(object)[]` or the `(object)array()` > shorthand is typically used when a generic object literal is needed. This > is also how php represents objects in var_export. > > ``` > php > var_export(new stdClass()); > (object) array( > ) > ``` > > Reasons: > - The ability to construct empty stdClass instances but not non-empty ones > is something users would find surprising, > and a lack of support for `(object)[]` be even more inconsistent if > factory methods were allowed in the future. > - stdClass is useful for some developers, e.g. in unit tests, when using > libraries requiring it for parameters, > when you need to ensure data is encoded as a JSON `{}` rather than `[]`, > etc. > - It would help developers write a clearer api contract for methods, > e.g. `function setData(stdClass $default = (object)['key' => 'value'])` > is clearer than `function setData(?stdClass $default = null) { $default > ??= (object)['key' => 'value']; ` > - stdClass may be the only efficient built-in way to represent objects > with arbitrary names if RFCs such as https://externals.io/message/115800 > passed > I'm not super convinced about the usefulness of (object)[] in particular, but I also think that we shouldn't artificially limit the types of expressions supported in constant expressions -- if there's no strong reason why something should be forbidden, it should be allowed. From that perspective, I think the root issue here is that constant expressions currently don't support casts at all. It's not just a matter of being unable to write (object)[], you also can't write (int)X (but you can write +X). I think it's perfectly reasonable to support casts in constant expressions, and if we do, then I don't think we need to go out of the way to forbid object casts either, so support for (object)[] should just fall out as a special case. Regards, Nikita --000000000000f1337105cd834708--