Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115436 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28655 invoked from network); 16 Jul 2021 09:40:32 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jul 2021 09:40:32 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0202C180502 for ; Fri, 16 Jul 2021 03:04:41 -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,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail.apserver.co.uk (server2.alteredperspective.co.uk [85.119.82.103]) by php-smtp4.php.net (Postfix) with ESMTP for ; Fri, 16 Jul 2021 03:04:40 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by mail.apserver.co.uk (Postfix) with ESMTP id B4EA04F4064 for ; Fri, 16 Jul 2021 10:49:57 +0100 (BST) Received: from mail.apserver.co.uk ([127.0.0.1]) by localhost (server2.alteredperspective.co.uk [127.0.0.1]) (amavisd-new, port 10024) with LMTP id JKi6B_kHo-Ar for ; Fri, 16 Jul 2021 10:49:56 +0100 (BST) Received: from [192.168.0.9] (maid-09-b2-v4wan-162707-cust865.vm41.cable.virginm.net [82.44.111.98]) by mail.apserver.co.uk (Postfix) with ESMTPA id 560204F4061 for ; Fri, 16 Jul 2021 10:49:56 +0100 (BST) To: internals@lists.php.net References: Message-ID: Date: Fri, 16 Jul 2021 11:04:35 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; 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-GB Subject: Re: [PHP-DEV] Readonly properties - immutability by default From: php.lists@allenjb.me.uk (AllenJB) On 16/07/2021 09:11, Eugene Sidelnyk wrote: > Thanks for your response! > Anyway, I probably put it wrong by saying "by default", so let me clarify > myself. > > What I really mean is omitting the dollar sign. So everything remains the > same with ordinary properties (which are mutable), and we introduce > immutable (readonly) properties as another type of them. > It looks like a great default: > ```php > public string name; > ``` > > So, once again, it has nothing to do with backward compatibility. No one > disposes the way properties are currently working. We introduce a new > _type_ or _kind_ of properties - readonly. > > Also I see useful future scope like readonly parameters: > > ```php > function foo(int firstParam, bool secondParam) > { > // no way to modify it, Error > firstParam = 23 * secondParam; > > return firstParam * secondParam; > } > ``` > > Yes, we can implement this with another keyword (again, `readonly`), but as > I see, only few people will use it because it is too complicated (really, > instead of simply declaring an argument, a programmer has to write a bunch > of other stuff in front of it for every single method and function). I do not like this suggestion because it's too subtle / non-obvious. Think from the point of view of an inexperienced developer who is reading through code and they come across this. What does it mean? What's different about it? They might not even notice the subtle different to other parameters, then get frustrated when the changes they've made don't work and they have to waste time going back to redo them. The readonly keyword in front of properties is clear. Without even reading the manual a new developer coming across this for the first time will have some idea of how they can expect it to work / how it should be used. I think that your "future scope" of readonly variables using the same no $ format likely has problems with constants and keywords. This would cause more problems when new keywords are added to the language. Even if the language itself doesn't have problems, I think it could significantly affect the readability of code in many cases. I think that a better suggestion would be to offer 'ro' as an (additional) alternative to 'readonly'. It's still shorter and I think that its meaning is likely to be understood / guessable by a significant proportion of (even inexperienced) developers.