Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116179 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68307 invoked from network); 27 Sep 2021 12:33:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Sep 2021 12:33:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2D29A180002 for ; Mon, 27 Sep 2021 06:15:33 -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.3 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_SOFTFAIL 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-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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, 27 Sep 2021 06:15:32 -0700 (PDT) Received: by mail-lf1-f45.google.com with SMTP id b15so76346980lfe.7 for ; Mon, 27 Sep 2021 06:15:32 -0700 (PDT) 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=auCUGPVLzupbMuBL2qliC9HO6O+zHKux26NFixzXPhU=; b=0GWPYM9c+CJ6GzCbTHVXqqpFO64Uj+Fe/vhHywyGbMdXS0BUtGKLz45gYSb9jARJBQ vSLNg2wNQoQ0e6xe5/1I8tbwzMHJTLDWuisopeoIvoJqp4vCn4WgMDthTyERSu3wqWfN nCUK7PsNZY26bnRcVHKzdzG2EuX45aLyIF7gXzPlfY8p4II5NsbJDTK2/kd+CoousTpg 0hAyL4HMBvyEAhDD+rYT0Ou0jNliVdOHuuUwfpxFrT/+UwdNWupcN3BlUC8byxtdRc1n 7ReK/1QrEE1IRGYVpiASVzlfS5kNIuQaCPPO8iJYPqQzhjyt42mdLnBJ3Gf0nJ6NnaMT ORcw== X-Gm-Message-State: AOAM531pfMA+rQlcoDVoPR4fFCTOXEUWl0qWzYShwEf12/jKSO1q/5Jg b0S6CJ7+2W77Oc5/nsaCzz653HZmX0CbeE38+gyOsQ== X-Google-Smtp-Source: ABdhPJwUQlMpLOVCtq/qpqKUgByDfrml9DUslDdSseUKT/N3EEIyIqbYp7Ql/Ethql605JtVRlkeV361zAyGQrHWcoY= X-Received: by 2002:a2e:bf21:: with SMTP id c33mr17686052ljr.101.1632748528206; Mon, 27 Sep 2021 06:15:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 27 Sep 2021 08:15:17 -0500 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000081b7205ccf9e4cd" Subject: Re: [PHP-DEV] Allowing `(object)['key' => 'value']` in initializers? From: pollita@php.net (Sara Golemon) --000000000000081b7205ccf9e4cd Content-Type: text/plain; charset="UTF-8" On Sat, Sep 25, 2021 at 10:45 AM tyson andre wrote: > 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. > > I think for consistency's sake, we'd want to provide a base constructor to stdClass to take an associative array that maps into dynamic properties, e.g. class stdClass { public function __construct(array $props) { foreach ($prop as $name => $val) { $this->$name = $val; } } } Then you could: php> static $x3 = new stdClass(['key' => 'value']); This avoids special-casing object cases in the engine while making it clear that a stdClass is what you wind up with, and honestly I think it'd be a useful pattern to use when initializing stdClass with defaults elsewhere. As to whether or not we SHOULD: While using declared props is preferred in the vast majority of cases, both for performance and for readability, there are legitimate cases where a short lived container wants the flexibility and convenience of an untyped, dynamic structure like an associative array with the visually clean aesthetic of object access. stdClass does that, and even if deprecation of dynamic properties passes it will continue to do that. I started this paragraph ambivalent about the feature, but I think I convinced myself around to a +1, though in the constructor form, not the cast form. -Sara --000000000000081b7205ccf9e4cd--