Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112300 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 54141 invoked from network); 26 Nov 2020 17:16:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Nov 2020 17:16:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E1D401804DB for ; Thu, 26 Nov 2020 08:42:56 -0800 (PST) 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.2 required=5.0 tests=BAYES_50,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 ; Thu, 26 Nov 2020 08:42:56 -0800 (PST) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id 6E0895101324 for ; Thu, 26 Nov 2020 16:42:54 +0000 (UTC) To: internals@lists.php.net References: Message-ID: Date: Thu, 26 Nov 2020 17:42:53 +0100 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] Method parameter promotion support From: pierre-php@processus.org ("Pierre R.") Le 26/11/2020 à 17:30, David Rodrigues a écrit : > Hello! > > It is just an idea to discuss. PHP 8 now supports parameter promotion via > constructor. Could this idea also work for common methods? > > public function setAge(private int $age) {} > === > private int $age; > public function setAge(int $age) { $this->age = $age; } > > The only problem I can see would be the difficulty of identification of the > possible properties, since it would be necessary to check all method > signatures to know about all possible properties. > > In this case, perhaps, it would be enough to allow the promotion only if > the property already exists. > > private int $age; // Required. > public function setAge(private int $age) {} > > Just to think about whether it is possible to think of something along > these lines, but I'm still not sure if it's a good option. > > > Atenciosamente, > David Rodrigues > It sounds weird for me, I can see two naive questions:  - what will happen if you don't call the method, will the property still exists on the object ?  - what will happen if you define it on more than one method ? And one more serious regarding code readability and maintainability: for me, class constructors and classic class property definition acts together as an index, that I can read to know what's inside a type structure ; if you start to explode that everywhere in arbitrary places code and I have to read and understand in order to use it, I will get angry very quickly, If I can't find property definitions neither in their rightful place, nor in the constructor. If you code looks like a puzzle that you have to solve in order to understand what it does, it's probably not that good. I don't like this idea. -- Pierre