Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116015 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 48652 invoked from network); 8 Sep 2021 19:21:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2021 19:21:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7B401180003 for ; Wed, 8 Sep 2021 12:58:59 -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-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 12:58:58 -0700 (PDT) Received: by mail-pf1-f182.google.com with SMTP id f65so2996406pfb.10 for ; Wed, 08 Sep 2021 12:58:58 -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=ReRO9W6FoIVspUbc8YrUNKheQzAGavCduZGWGX77sdg=; b=q4puESvXyuduDYm4svJETIkvoZPQHNEPGUkgESPVpCnIy/uNZ8tC+hYGqOIgtN92YA ameg/4duj5/9ieuKe+Nm7bqMi3FYtrR3Zwx62A1NLbeYWRC0RmeK2ChPyABK4EnWcrkG cfNRYoUsaDO6Xf62b/D0ZueiyBUCs3hOUeM66XILDCQy7ZlWvYQ2PW+HuVc9oBbXdL0A whoywJhjn0bwkXFObOFPf55vWBlWLOi/NA74iR6WDWajrpt3QwUAEkmmTpKLZXqz3SCw GDYWU7b+QXqdqJqknA8JhYIqEb6mRR3LP1rhCnrIjiIZEf1K1gm5MqYhgu6jRM5OR10m 3yGw== 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=ReRO9W6FoIVspUbc8YrUNKheQzAGavCduZGWGX77sdg=; b=07HpvrvmmhndGpFHgYZl8CmR4Pdy5Q6pmcSXoaMs1gjxrfhJCQgi6BqkmLltD3aN6j roX4vHdjlvsxbGsOOOZHSLCPRSmMmmVgWRJFue+KNjNIn4WryH2HneKOCp68TTzBYQxu TlwyUYUtAXVDdoI5golL4gnZ1baQyboMaMoq3Gv1TW+Stu6ui301CoawqqEQDmuNyQvB YO2YEQVn3RWnVEaz20HPMchz+1M/mPIHQSmWM8AByIlQvgk9XRIERMH0g7kgRJe+v2tk ymGt6eAvFOpWAfOcf3Sv8AbrOp3EhvKqyGW5UBh95eAAvw8z2iejMewevJfya6fun/Z5 CBjQ== X-Gm-Message-State: AOAM533pm/2agVFCVkDExo5U1oTQI86fQf+ZQ8AMyvGMixBxiZPWadIF 3/HKhMI/VjmXZRRiR8+ZuvHnOVkiar2UwL+954dXVg== X-Google-Smtp-Source: ABdhPJxPzVL0fdSSj/gtGGjo/lrItOSgoH2uT4d/zmlHxPZ07wiBhS74fRmq8T7bWUVxQ6iBqFyHUu8tnfCbABVq+/o= X-Received: by 2002:a63:f963:: with SMTP id q35mr5310367pgk.132.1631131135210; Wed, 08 Sep 2021 12:58:55 -0700 (PDT) MIME-Version: 1.0 References: <46f2e842-8608-0622-f1c1-3eb746658c84@gmail.com> In-Reply-To: Date: Wed, 8 Sep 2021 21:58:44 +0200 Message-ID: To: Lynn Cc: Mike Schinkel , Rowan Tommins , 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 18:33, Lynn wrote: > > On Wed, Sep 8, 2021 at 5:38 PM Mike Schinkel wrote: > > > A couple more things; add a `JSON_OUTPUT_DYNAMIC_OBJECT` flag to output to > > DynamicObject for `json_decode()`, add a 3rd parameter for flags to > > var_export() for the same reason, a `'return_dynamic_object'` option for > > `unserialize()`, and so on. > > > > It would also be interesting to enter a user-defined class here to work as > a reversed JsonSerializable. Could be a static factory method for all I > care, and would omit the requirement of serialization libraries for > "simple" things where you still want decent object typing. Could also work > together with interfaces accepting properties, effectively making the > result of json_decode an anonymous class that still adheres to an > interface. In case of the custom 'deserialization' you can throw exceptions > if the format is not correct. In the case of an anonymous class with an > interface it could throw an exception if the structure doesn't match > (possibly controlled by flags?). I think we want round-trips of json_decode() + json_encode() to work loss-free, without any custom classes or extra parameters. Currently, '{}' and '[]' have different results with json_decode(). assert(json_encode(json_decode('{}')) === '{}'); // Perfect round-trip. assert(json_encode(json_decode('{}', JSON_OBJECT_AS_ARRAY)) === '[]'); // Lossy round-trip. This can only work if we have a built-in data transfer object, distinct from arrays. Currently this is \stdClass, using the dynamic property mechanism. But one could easily imagine a different kind of data transfer object, which would use a different mechanism instead of dynamic properties. A native implementation of JsonSerializable does not really work here, because ->jsonSerialize() would have to return \stdClass to result in '{}'. Instead, what about something with a "->getData(): array" method, but then json_decode() would encode it as '{}'? assert(json_decode('{}') instanceof DataTransferObject); assert(json_decode('{}')->getData() === []); assert(json_encode(json_decode('{}')->getData()) === '[]'); assert(json_encode(json_decode('{}')) === '{}'); For the proposed rename: - If we can fully deprecate and replace dynamic properties long-term, I would rather keep the name \stdClass until it dies. - Instead of an alias or rename, I would rather have a new data transfer object class which would not rely on dynamic properties, but on a new mechanism. Imo, creating an alias won't actually make life easier for anyone, unless we can fully remove and replace \stdClass. It would mean that developers need to learn both names, and understand how aliases work - something you don't really need to learn otherwise. Think of the silly wtfs that can occur if people don't fully understand aliases, or are not aware that DynamicObject is just an alias, while \ReflectionClass and get_class() still return 'stdClass' as the class name. -- Andreas