Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126043 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 14C921A00BD for ; Sat, 23 Nov 2024 22:22:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1732400727; bh=Ghdr55xZLUOeIoHyI5jaGak44KqshgqVYb2ZzR7EckQ=; h=Date:Subject:To:References:From:In-Reply-To:From; b=YVdZx+stclcfwqKRxBm6KPYB9uRYi4gh6WlX5/07/o8KQwjKpl9FmLQZdHWhqq3Xn BGLFOYcyRc66IMG/C7LQZqqT7i2J8s3dqZ7feS3d5Cwu/1dUCDBzHT5Hj4fnYbXT20 yHSsXF5WwVaLBh8W/MdscyawbK/lBL4myfPCo2cIFIP72HEkX7+MvyZo3AKR5kS6Us SmQCV3XEaz8/5Gab5Eky8ZjMBwswkBQh91+h8a8ZJ1k8W0UVdoFHQtzm26NCklt01/ IiAow6JXiYToBGVyhibx9sJpX2GDqUrPz5ZP4CJ1fjucBE8BNVWxTnbAJqCe2gJUFB I55ItnFAkzbtA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 30B95180062 for ; Sat, 23 Nov 2024 22:25:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 23 Nov 2024 22:25:25 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id D542E13801D9 for ; Sat, 23 Nov 2024 17:22:42 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Sat, 23 Nov 2024 17:22:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1732400562; x=1732486962; bh=2fya7zq4SB kTiASWTZZw+aYDC7sKITGFFYLbKifNiHA=; b=AmSt5YDqfi1X0X1H2cynRYA7wc AQU1nSo99LT4IjYX64IstsiHpzPGGMJi3Bt+QC22aq0f0dsDeh5srExJUdBcUrZy jvjARTXqgDuQufIQIRJLvYQ4R7ig4NPK/JzvUR4OUCABqsPDZEIoJnWn0cfM1wRI OVzN/m0htvmygqECAKh/WDhsfqoSzCzmeEYK+/Y/BGmX3BhW8cLZGFsLIelnLEXB WybCFy9zI+VxwmLIEd9diK//JnOP1TEas64LviunihBHTrL2eklGL4y0kTxgZLry dXc18jJatMXCVwUB5ytpKlSNa31Ke/JUpir8NAmHf0CsQZr6/ypyEXLFvX2Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1732400562; x=1732486962; bh=2fya7zq4SBkTiASWTZZw+aYDC7sKITGFFYL bKifNiHA=; b=hNS/2/tpQkBA6PNOSkDIGOL2RuNzaZr8rISRNVTC7jHK547fb0x ZaVV+zqZLSlPKpsyjbAYmTC/9MAJlLpCGb0Y/iGynM7fpTaFQwCLbnLsb1MwaxsG XsG4+mb0owW40NmcI2GKMblMALxgHRKes65ZHVLv6Ea3GTiVKPaBm87vxtrucBCt SQjPSG8cNIj1ESI63vpqxARQq0bc+MUHcAMwZHpuNcZPyrZc7nlHInyjoPBlWtX3 Q3PflyuuFyJcQuuy9qCPRKdgQegP7YG8xHwYW0+CTpRr/sds0NewF1NUn3RSuxII s/TYbFgcr2BkptJ1MzELhd5jEQgx7anJieA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrgedugdduheelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgg gfuffvfhfhjgesrgdtreertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhs ucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecugg ftrfgrthhtvghrnhepheetleeiiefgueduieeuieffvdevheduueefkeejuefgffeftdei tdegtedtleetnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhkpdhnsggprhgtphhtthhopedu pdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsth hsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 23 Nov 2024 17:22:42 -0500 (EST) Content-Type: multipart/alternative; boundary="------------0oggCEiCP4jqYRsMak1oMvnz" Message-ID: Date: Sat, 23 Nov 2024 22:22:40 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Data Classes To: internals@lists.php.net References: <18b85ba5-5f1c-489c-9096-3ae203977fbe@app.fastmail.com> <163a08c1-2279-4a3f-a1bb-8cdfe406a4ba@rwec.co.uk> <0b87a3eb-7e63-4e28-a11d-25824e240a73@app.fastmail.com> <2c286462-a9ac-4b12-89bf-aea66680dea0@rwec.co.uk> <3e4506d7-227e-4521-a0c8-68e71fdc42a1@app.fastmail.com> Content-Language: en-GB In-Reply-To: <3e4506d7-227e-4521-a0c8-68e71fdc42a1@app.fastmail.com> From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------0oggCEiCP4jqYRsMak1oMvnz Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 23/11/2024 20:27, Rob Landers wrote: > Interesting! I actually found it to be intuitive. > > Think of it like this: > > function increment(array $array) { >   $array[0]++; > } > > $arr = [0]; > increment($arr); > echo $arr[0]; // is 0 > > We don't expect $arr to be any different outside of the function > because $arr is a value, not a reference. My mental model, rightly or wrongly, is that passing to a parameter is a bit like an assignment to a local variable: function increment(array) {    $array = $__args[0];    $array[0]++; } (This is explicitly how subroutine parameters work in Perl; I don't know if that's affected my mental model, or just means Larry Wall pictured it the same way.) You can even assign a new value to it, like any other variable: function whatever(array $array) {    $array = 'not even an array any more'; } But in PHP, $this isn't a parameter, and it's never possible to assign a new value to $this; so it feels completely alien to have a method where $this stops referring to the current object, and becomes a local variable. >> I think it would be clearer to prevent direct modification of $this: >> > > Not that I disagree (see the records RFC), but at that point, why not > make data classes implicitly readonly? I'm only suggesting restricting mutation on $this, not on the object itself. $foo->x++ would still work, and have automatic copy-on-write; but $this->x++ would be an error on a data class, just as $this=$bar is an error on all existing objects. >> That would still be compatible with Ilija's suggestion, which was to >> add special "mutating methods": >> > I actually find this appealing, but it is strange to me to allow this > syntax on classes. Is there precedent for that? Or is there a way we > can do it using "regular looking PHP"; or are structs the way to go? The way I see it, it's just a third type of method, to add to the two we already have: - instance methods: $this refers to the current instance - static methods: $this is forbidden - mutating methods: $this refers to the desired result of the mutation In fact, it's a bit like __construct or __clone, where $this refers to the newly created/copied object, before anything else points to it. -- Rowan Tommins [IMSoP] --------------0oggCEiCP4jqYRsMak1oMvnz Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 23/11/2024 20:27, Rob Landers wrote:
Interesting! I actually found it to be intuitive.

Think of it like this:

function increment(array $array) {
  $array[0]++;
}

$arr = [0];
increment($arr);
echo $arr[0]; // is 0

We don't expect $arr to be any different outside of the function because $arr is a value, not a reference.


My mental model, rightly or wrongly, is that passing to a parameter is a bit like an assignment to a local variable:

function increment(array) {
   $array = $__args[0];
   $array[0]++;
}

(This is explicitly how subroutine parameters work in Perl; I don't know if that's affected my mental model, or just means Larry Wall pictured it the same way.)

You can even assign a new value to it, like any other variable:

function whatever(array $array) {
   $array = 'not even an array any more';
}

But in PHP, $this isn't a parameter, and it's never possible to assign a new value to $this; so it feels completely alien to have a method where $this stops referring to the current object, and becomes a local variable.


I think it would be clearer to prevent direct modification of $this:


Not that I disagree (see the records RFC), but at that point, why not make data classes implicitly readonly?


I'm only suggesting restricting mutation on $this, not on the object itself. $foo->x++ would still work, and have automatic copy-on-write; but $this->x++ would be an error on a data class, just as $this=$bar is an error on all existing objects.


That would still be compatible with Ilija's suggestion, which was to add special "mutating methods":

I actually find this appealing, but it is strange to me to allow this syntax on classes. Is there precedent for that? Or is there a way we can do it using "regular looking PHP"; or are structs the way to go?


The way I see it, it's just a third type of method, to add to the two we already have:

- instance methods: $this refers to the current instance
- static methods: $this is forbidden
- mutating methods: $this refers to the desired result of the mutation

In fact, it's a bit like __construct or __clone, where $this refers to the newly created/copied object, before anything else points to it.


-- 
Rowan Tommins
[IMSoP]
--------------0oggCEiCP4jqYRsMak1oMvnz--