Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115708 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 12410 invoked from network); 13 Aug 2021 15:22:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Aug 2021 15:22:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EFDF61804B3 for ; Fri, 13 Aug 2021 08:53:39 -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=-2.7 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS35329 80.237.132.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wp160.webpack.hosteurope.de (wp160.webpack.hosteurope.de [80.237.132.167]) (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, 13 Aug 2021 08:53:39 -0700 (PDT) Received: from [2a02:8109:9d40:1d44:40b6:f5d3:c567:d46c] (helo=nas.fritz.box); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1mEZV3-0002WR-9j; Fri, 13 Aug 2021 17:53:37 +0200 To: internals@lists.php.net References: <42b262eb-0d9b-8feb-cc3d-da3e36e14d1e@mabe.berlin> Message-ID: <5f05e230-4f19-aa5b-73c4-78dd52e95764@mabe.berlin> Date: Fri, 13 Aug 2021 17:53:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1628870019;8e7062a9; X-HE-SMSGID: 1mEZV3-0002WR-9j Subject: Re: [PHP-DEV] readonly properties From: marc@mabe.berlin (Marc) On 8/13/21 4:56 PM, Nikita Popov wrote: > On Thu, Aug 12, 2021 at 9:16 PM Marc wrote: > >> Hi, >> >> As 8.1 adds readonly properties I wonder which build-in properties >> should be defined readonly. >> >> Currently I could find build-in readonly properties only on PDO and DOM. >> >> >> Very incomplete list where readonly properties could make sense: >> >> 1. Enum properties: >> >> enum Test:string { >> case TEST = 'test'; >> } >> >> $case = TEST::TEST; >> $refl = (new ReflectionObject($case))->getProperty('value'); >> var_dump($refl->isReadOnly()); // false >> var_dump($refl->isPublic()); // true >> $case->value = 'foo'; // Fatal error: Uncaught Error: Enum properties >> are immutable >> > Yeah, these are a perfect use case for "readonly". Done in > https://github.com/php/php-src/commit/caefc6a50789295b0993c4e657c825484650172a. > This actually fixes a bug, because the homegrown "readonly" implementation > for enums was not quite correct. WOW this was fast. Less code and even a bugfix +1 > >> 2. DateInterval->days >> >> $interval = (new DateTime())->diff(new DateTime()); >> var_dump($interval->days); // 0 >> $refl = (new ReflectionObject($interval))->getProperty('days'); >> var_dump($refl->isReadOnly()); // false >> var_dump($refl->isPublic()); // true >> $interval->days = 2; >> var_dump($interval->days); // 0 >> > The DateInterval properties are currently implemented as getters/setters on > some internal state. Some of those are getter-only, but probably not fully > immutable. I was referring to the property "days" only as this is a non writable value generated on "DateTime->diff". As you can see in the snipped it doesn't allow to modify days but it also doesn't fail which seems wrong to me. I don't have much knowledge about internals but would it be possible to define "public readonly int|false $days" not using getters/setters and keep all other properties as it? > > 3. Exception properties >> Exception properties are protected but does it really make sense to be >> able to modify an exception property after initialization? >> >> I know this would be a BC break :( >> > There is definitely code out there relying on modifying both protected and > private Exception properties, I don't think we want to touch these without > cause. Yea you are probably right - it would be a heavy BC break for not much profit. > Regards, > Nikita > Thank you very much Nikita!