Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112110 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47863 invoked from network); 23 Oct 2020 19:15:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Oct 2020 19:15:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DC4EE1804B5 for ; Fri, 23 Oct 2020 11:33:13 -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.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 11:33:13 -0700 (PDT) Received: by mail-wr1-f53.google.com with SMTP id x7so3207129wrl.3 for ; Fri, 23 Oct 2020 11:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=RaG0CsP7LIE3ToZiTmHZ36dv8d3IT+fxto0ZAH4Ltvo=; b=PYbM7defundChJkuJljYc8hl2oNojRQ1MERdxOrWB39CY6byEKW5AFlKWjM0giREVo S2BXmd8JYnSycwpJhfeEjUL8xzmNlSn9/kGj0ad1S9BJQuPy9bQppvsUWDuqIq6/Qa/3 qLqju+YoZFEjRKzTgoYnqXjvOBgVJ1YGxBEFnN/V9RW5CYZ1LZqq96J5VAVYGrAghEZZ mYcRSPj9C/FSAz8wEtWTyoBmFZALunfj6P9sCAQM3YtARYgBJLibiEHKEODOfbrBdYvk upRvMrSpgc6ZgWZ2cJaRLqeI+hfukfbkNw5u1HUErAsfWKdMAQNb/aytWfVedf1ByLM9 ilqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; 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=RaG0CsP7LIE3ToZiTmHZ36dv8d3IT+fxto0ZAH4Ltvo=; b=glToVPE6+C0rmnad4eTzyOLdikekbwaCoGAPbZG6+XdmX67zmE8a0wX7K7mtiCzK4M a/IxS3ycvgf0oDqQRaBfaFnylPlPX95ihvG+EfoFRD8uipZxJchEfrU5xinG+Y+VxuT0 V3c4kcl9MJ41/MfXbHkEg5eZjk+XmUjUKxFZEU3mQ12TLMGljMNSASnY/gQK9/kVnjiZ Va9Zkm/7gfcYpjDj0DQdc6kFvkWTJ48LuY7j7iUiUTyGAWLxdoaUseB1Oo63BptdOwqR 2LBZPfBozRXnZWv5709pJNAvpBzpntHD/Znmw+VwRvNCpFJZNnKNQPhpG8Nvn/ms+qsJ 6upQ== X-Gm-Message-State: AOAM531ZVa8q+oyXgMMAunFiodMeAa7UvXrFabtT7tvqWVMgZKUtpmVn xCVBVV00j2/PK2cKHvHU/s1aFyn+xl0= X-Google-Smtp-Source: ABdhPJzMcNJqZT1uG+WETOAp+MfdIIoKoNug5LRbAuFxP9tAp2N7g80afej8VEXU3yYqDbR2mbuCJQ== X-Received: by 2002:adf:cf04:: with SMTP id o4mr3950613wrj.244.1603477991474; Fri, 23 Oct 2020 11:33:11 -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 x22sm5547904wmj.25.2020.10.23.11.33.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Oct 2020 11:33:10 -0700 (PDT) To: internals@lists.php.net References: <5f92f1e0.1c69fb81.a800f.cbe1SMTPIN_ADDED_MISSING@mx.google.com> Message-ID: Date: Fri, 23 Oct 2020 19:33:10 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 MIME-Version: 1.0 In-Reply-To: <5f92f1e0.1c69fb81.a800f.cbe1SMTPIN_ADDED_MISSING@mx.google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Re: Is there any interest for object constructor shorthand *just forstdClass* From: rowan.collins@gmail.com (Rowan Tommins) On 23/10/2020 16:08, Andreas Bittner wrote: > Closing I would like to make a short point with regards to the > `(object)` cast: > Both a cast of \stdClass to `(array)` and a cast of arrays to > `(object)` are common use cases in (JSON-)API development. It is not > unheard of for API signatures to contain certain properties of the > JSON response object only if some conditions are met. Currently the > best way to achieve this is either by dynamically adding properties to > an existing \stdClass object, or by populating an array and `(object)` > casting it later. 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? 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. 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: function array_to_object($arr): object {     $obj = new class {};     foreach ( $arr as $key => $value ) {         $obj->{$key} = $value;     }     return $obj; } The only difference I'm aware of is that you can no longer use "stdClass" as a type constraint, but I'm not sure what value that would ever have. Indeed, with anonymous classes you could trivially add a much more meaningful type constraint using an empty interface: instead of: function getFromDB(...): stdClass {       $result = new stdClass;       foreach ( $this->blah()  as $column => $value ) {             $result->{ $column } = $value;       }       return $result; } you can write: interface DynamicDBItem {} function getFromDB(...): DynamicDBItem {       $result = new class implements DynamicDBItem {};       foreach ( $this->blah()  as $column => $value ) {             $result->{ $column } = $value;       }       return $result; } Regards, -- Rowan Tommins (né Collins) [IMSoP]