Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116268 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73236 invoked from network); 13 Oct 2021 07:33:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Oct 2021 07:33:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 06435180382 for ; Wed, 13 Oct 2021 01:19:51 -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,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-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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, 13 Oct 2021 01:19:50 -0700 (PDT) Received: by mail-wr1-f48.google.com with SMTP id k7so5358665wrd.13 for ; Wed, 13 Oct 2021 01:19:50 -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=tnnIDlghAcHsn1kiN45i9JJ249d3kOwJ9w78Xhr6YKk=; b=La7pRZQAT06FDCvYnSEUni+VP1R7IntUsykY2RIwWXi+FuFyk+g5JcZgWpB5tbHVfo r4mLahx5OQ3Kr3l6xU7J9eMNPXQVU0jV7NYLPAQMAe2SJSQeOBoUtukL36E8yN7YBQjf i8z1b9Ksgx/y/NzH6FybNJgG1MGd7b9VKKDOX0NhcESJsah2HXKV/v5vduSh53VBlcWu TOp20c0lT66luTIaZ5Wrk8+xDpVzXtPTb5Yhmx7XmVJrjxeS4hEWHFupl+KaN/BoFR/Y yJXMG76XOX8cZ4mcNdg0cn/exxbAaDmq2Lc8xJHlIf18+XaETpfdC21iiCZSyj18D3Q8 tQVA== 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=tnnIDlghAcHsn1kiN45i9JJ249d3kOwJ9w78Xhr6YKk=; b=1oW5UYmSTeQ+6smkzQ0zBI8N7wOKDM371Co8Op8PYa6B84DF5KecxFbpdxmFI231iq 6r8YpPpKZjNG0b0ak6w+a7WzvJgVnAqzI4V7WJnKtGvgLkZHuVeRQ9tBbvoRNx9uFMQR JmPpkgDHoAt7JPIRLzk5buhmeoUSyeAj9OsMKKI6BuDIs/5090v4NmSzYJ+hQwxuIinT rpJFsdW/aUhSF+3pNIQsBxmhVLiAW8FeinulOgey/LxhmCQ8tjjrT4ArtUUqa0MVGF+W xCY7SAKi7N/t1kWk0cbnn4BhzR9TAO5jeew3iZ6YdBI1C4cJSFMsuRcaIu0F1yYSmkoC i3Fw== X-Gm-Message-State: AOAM5313XQkrq9Lbg5nwBhjgM4Ltn0Wh1lsyAGf5dT51o0S90q1IKNZG 9N0wn1dB1hs7dEq+Asa+5fIjisElZmQ= X-Google-Smtp-Source: ABdhPJy+Ds5lLSsAEb23/Xjx38AmrgrOFOI9YxtaaDfnvUuq3AofOFFYHl9seWhLPGejXqdkh34mBg== X-Received: by 2002:adf:b348:: with SMTP id k8mr37789248wrd.435.1634113188452; Wed, 13 Oct 2021 01:19:48 -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 d7sm12799239wrh.13.2021.10.13.01.19.47 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 13 Oct 2021 01:19:47 -0700 (PDT) To: internals@lists.php.net References: Message-ID: Date: Wed, 13 Oct 2021 09:19:47 +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] Re: [RFC] Deprecate dynamic properties From: rowan.collins@gmail.com (Rowan Tommins) On 13/10/2021 02:43, Tim Starling wrote: > I think it would still be the biggest compatibility break since PHP > 4->5. I think this is a rather large exaggeration. It's certainly a use case we need to consider, but it's not on the scale of call-time pass-by-reference, or removing the mysql extension, or a dozen other changes that have happened over the years. > As the RFC notes, you could migrate to WeakMap, but it will be very > difficult to write code that works on both 7.x and 8.2, since both > attributes and WeakMap were only introduced in 8.0. Firstly, writing code that works on both 7.x and 8.2 will be easy: ignore the deprecation notices. As discussed elsewhere, treating a deprecation as a breaking change makes a nonsense of deprecating anything. Secondly, I would be surprised if libraries using this trick don't already have helper functions for doing it, in which case changing those to use WeakMap and falling back to the dynamic property implementation on old versions is easy. Maybe I'm missing some difficulty here? if ( class_exists('\\WeakMap') ) {     class Attacher {         private static $map;         public static function setAttachment(object $object, string $name, $value): void         {             self::$map ??= new WeakMap;             $attachments = self::$map[$object] ?? [];             $attachments[$name] = $value;             self::$map[$object] = $attachments;         }         public static function getAttachment(object $object, string $name)         {             self::$map ??= new WeakMap;             return self::$map[$object][$name] ?? null;         }     } } else {     class Attacher {         public static function setAttachment(object $object, string $name, $value): void         {             $attachments = $object->__attached_values__ ?? [];             $attachments[$name] = $value;             $object->__attached_values__ = $attachments;         }         public static function getAttachment(object $object, string $name)         {             return $object->__attached_values__[$name] ?? null;         }     } } $foo = new Foo; Attacher::setAttachment($foo, 'hello', 'world'); echo Attacher::getAttachment($foo, 'hello'), "\n"; > I would support an opt-in mechanism, although I think 8.2 is too soon > for all core classes to opt in to it. We already have classes that > throw an exception in __set(), so it would be nice to have some > syntactic sugar for that. I proposed "locked classes" a while ago, but consensus was that this was the wrong way around. One suggestion that did come up there was a block-scopable declare which would allow manipulating dynamic properties, even without the target class's co-operation. However, I think WeakMap (with a helper and a fallback like above) is probably a superior solution for that, because adding properties outside the class always carries risk of name collision, interaction with __get, etc Regards, -- Rowan Tommins [IMSoP]