Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115434 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 26041 invoked from network); 16 Jul 2021 09:38:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jul 2021 09:38:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3ECF9180089 for ; Fri, 16 Jul 2021 03:03:06 -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.5 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (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, 16 Jul 2021 03:03:05 -0700 (PDT) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id 430A75101326; Fri, 16 Jul 2021 10:03:03 +0000 (UTC) To: Eugene Sidelnyk , Nikita Popov Cc: PHP Internals References: Message-ID: Date: Fri, 16 Jul 2021 12:02:59 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] Readonly properties - immutability by default From: pierre-php@processus.org (Pierre) Le 16/07/2021 à 10:11, Eugene Sidelnyk a écrit : > 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). Hello, I'm very happy that the readonly properties RFC did pass, I waited for this for a long time. I like the "readonly" explicit keyword, explicit is always better than implicit for disambiguation and readability. I'd wish having per-default readonly properties as well, but this is PHP and fundamentals cannot be changed just like that, it's not Rust, or any other language, PHP is PHP. Explicit "readonly" is in my opinion the best option, it's easy to type, and it's easy to read, and it's the same keyword in many other languages. On the opposite side, omiting the $ sign seems like a huge mind-fuck, easy to miss-read, easy to miss-type, error prone and obscure. Regards, -- Pierre