Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123808 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 0EF111A009C for ; Tue, 25 Jun 2024 07:50:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719301932; bh=uwuVdAy4DMttXuCs8529Nl2djN+KEZhgRZLtYCGBF/E=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=gkUWTjb0K5Sm1dthP5nxRwkKYGGpZdvUw4HOPcILioGlk66rbErgdrEhWImSaMaLf 7itkFaoTI+u7de1TWSfjknJyJQcr1GmaUAuDIrVZtQ8dRaMtiKfB1Sxb66G3+VFaUt H8QwV8Ri/GHrTgSnaQUQi53UyAgxFnH4vxacc/Pg/63hAjHr54txeB6F3poocmUEzf 63JoafliWZxC/B29AvAz8uvZkhMdB/sbSFgF0QwnTadSheNgTkCPgFHpXJF6k60D7B G70oQPOpEN7Zs9GBehuG0FsrvseLoaYzG76gBuqNel5GcLlXgbjekUhJfLs5uN/bMo frfU6WI/DbTBA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BCD141809E4 for ; Tue, 25 Jun 2024 07:52:09 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 ; Tue, 25 Jun 2024 07:52:09 +0000 (UTC) Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a6fe118805dso284450166b.3 for ; Tue, 25 Jun 2024 00:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719301852; x=1719906652; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=7yjrF9B6P9EhFdNWN6WngwelLnYZgXLN1Xp+fnu6S9k=; b=bHjDb17VW/pxhQAlfaKiNHIEhIntVQ2BcCLGj09qTcL31TkG7AXjmgK5uRUPcvvMTU t6sq5G/rX+xzMauL8VBHVpypg548o6K71N2eT6ACIaMTjfa+hZxaFZRNhS3xcXFzyN5R co7BoieoJo3W+n49lzI8mMcDmkQRjQXFyqzSF87D1HB0KexieIfsu9aq2FWuAur+znP4 1dfRIfluXgN9dFf/1WTBCmjCNWunkqbwVDmtwWNNJEGrDtauo+ZE/ziZ8s/dWGMvHhh6 G//xDHz8YVNp6oDbBgk7h6+Rx5YhJOVJ08sRSvB9coPE/3S5lKfrGQu98+NdNUkpWubT R+9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719301852; x=1719906652; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7yjrF9B6P9EhFdNWN6WngwelLnYZgXLN1Xp+fnu6S9k=; b=bxIl11xjMAirVmN30ksDy55h8NTYpT/BrzkUM8u7O8+zUHsjjBJQbDkCyzHw8RSAtI pzdBKP828yt+HzJyiog5rXNiJdh2GN540q4qN5bPponPI9RsdF2voBuaOWuZp6OAOTYZ UO8LdkyTpHhEK+Wt/R0ZpvW3XY61Wj1YP44hQr2PxuX0NtmptD99R04Rh4gIMPCJxzQX O7qXQfYHmg3/8tqoY5Kq/go3nTWRpbGsmFXipxTEOFWUYq9SkpREJexlQCmkjGlZ3rLH P4rfYnV4nPieobtr4DgeQE7L/d9yCahpm+a5RUtvE1uAvh1EFtaQkg3Wq3WzaFwvSrHb PbKQ== X-Gm-Message-State: AOJu0Yx+DG22Xe4yXoh1Gr1+1ADCzh2K9wsjkJQPiCl6I06I9TbkGAE8 UIJOYSyxurAAOlXnXxsesdVX5ssa39r/f51XlxOW+Qdz7cL7NhtkLcIFFciQlfxi1LeUKPYQHoe nsMEj8yGWi2Hy6amHEqzgwK1uCALLE1Vg X-Google-Smtp-Source: AGHT+IGgMru+ZWLw20t4/xIQPirTyGuLwaOX5+Ds3pqLXdDi3lwbD5WCPwfj8bH4pxaJpVeeldHn3O3rfZpyCsBYUYQ= X-Received: by 2002:a17:906:714c:b0:a72:40cc:22cb with SMTP id a640c23a62f3a-a7245ccdda4mr398100566b.21.1719301486534; Tue, 25 Jun 2024 00:44:46 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 25 Jun 2024 09:44:34 +0200 Message-ID: Subject: Re: [PHP-DEV] Add $this return type To: radar3301 Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000005de870061bb2125e" From: cardamoneluigi@gmail.com (Luigi Cardamone) --0000000000005de870061bb2125e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jun 25, 2024 at 2:29=E2=80=AFAM radar3301 wro= te: > I'm looking for initial feedback on the following proposal. > > Often, we have the following (fluent interface) setter method: > > public function setName(?string $name): static > { > $this->name =3D $name; > > return $this; > } > > I propose to add "$this" as a return type, ie: > > public function setName(?string $name): $this > { > $this->name =3D $name; > // implicit return $this; > } > > public function setName(?string $name): $this > { > $this->name =3D $name; > // this would be an (compiler?) error > return $somethingOtherThanThis; > } > > public function setName(?string $name): $this > { > $this->name =3D $name; > $self =3D $this; > // this would not be an error > return $self; > } > > public function setName(?string $name): $this > { > $this->name =3D $name; > // technically useless, but not an error > return $this; > } > > public function setName(?string $name): $this > { > $this->name =3D $name; > if ('foobar' =3D=3D=3D $name) { > // not an error to return early > return; > } > // do some other stuff > > // any of the above legal examples > } > > It should be obvious, but functions outside of a class context would not > be able to use this syntax. > > BC: > There would be no BC breaks, as this syntax is currently invalid (Parse > error: syntax error, unexpected variable "$this"). > > Other Considerations: > With regards to reflection and inheritance, "$this" would be considered a= n > alias for "static". > > Regards, > radar3301 > I strongly agree that an alias for the "static" keyword is really necessary to improve the semantics of the language. The word "static" is used for many things: static methods, static classes and Late Static Bindings (that is the case that you are discussing). I think that in the case of Late Static Bindings (e.g. `new static()` or return type `..(): static`), the word "static" does not give any clue about the meaning of that code. Thus, I like your proposal. The part that I do not like is the implicit return: it may confuse developers and static analysis tools. Is it possible to replace "$this" with "this"? Cleaner and coherent with "self". Regards, Luigi --0000000000005de870061bb2125e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Tue, Jun 25, 2024 at 2:29=E2=80=AF= AM radar3301 <radar3301@gmail.com= > wrote:
=
I'm looking for initial feedback on the following= proposal.

Often, we have the following=C2=A0(fluent in= terface) setter method:

public function setName(?string $name):= static
{
=C2=A0 =C2=A0 $this->name =3D $name;

=C2= =A0 =C2=A0 return $this;
}

I propose to add &qu= ot;$this" as a return type, ie:

public function setName(= ?string $name): $this
{
=C2=A0 =C2=A0 $this->name =3D $name= ;
=C2=A0 =C2=A0 // implicit return $this;
}
<= br>
public function setName(?string $name): $this
{=
=C2=A0 =C2=A0 $this->name =3D $name;
=C2=A0 =C2=A0 // this= would be an (compiler?) error
=C2=A0 =C2=A0 return $somethingOth= erThanThis;
}

public function setName(?string $name= ): $this
{
=C2=A0 =C2=A0 $this->name =3D $name;
=C2= =A0 =C2=A0 $self =3D $this;
=C2=A0 =C2=A0 // this would not be an= error
=C2=A0 =C2=A0 return $self;
}

publ= ic function setName(?string $name): $this
{
=C2=A0 =C2=A0 = $this->name =3D $name;
=C2=A0 =C2=A0 // technically useless, b= ut not an error
=C2=A0 =C2=A0 return $this;
}

public function setName(?string $name): $this
{
=C2= =A0 =C2=A0 $this->name =3D $name;
=C2=A0 =C2=A0 if ('fooba= r' =3D=3D=3D $name) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // not an e= rror to return early
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 // do some other stuff
=
=C2=A0 =C2=A0 // any of the above legal examples
}
=
It should be obvious, but functions outside of a class conte= xt would not be able to use this syntax.

BC:
=
There would be no BC breaks, as this syntax is currently invalid (Pars= e error: syntax error, unexpected variable "$this").
Other Considerations:
With regards to reflection and= inheritance, "$this" would be considered an alias for "stat= ic".

Regards,
radar3301
=

I strongly agree that an alias for the &qu= ot;static" keyword is really necessary to improve the semantics of the= language.
The word "static" is used for many thin= gs: static methods, static classes and Late Static Bindings (that is the ca= se that you are discussing).

I think that in the c= ase of Late Static Bindings (e.g. `new static()` or return type `..(): stat= ic`), the word "static" does not give any clue about
th= e meaning of that code.
Thus, I like your proposal.
The part that I do not like is the implicit return: it may conf= use developers and static analysis tools.

Is it po= ssible to replace "$this" with "this"? Cleaner and cohe= rent with "self".

Regards,
Luigi


--0000000000005de870061bb2125e--