Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123798 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 A00F61A009C for ; Tue, 25 Jun 2024 00:25:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719275213; bh=dvjKBU4bfswf7eSviL8PMiPbFjaAzfFqJPZEgTfikok=; h=From:Date:Subject:To:From; b=hwjC5ablZEp/Syv3KPpmAfSwnLL7QDqe8E19X7dyXfFKfUVBLcGY5b/7KOAzPTHbQ oHfVrxj8eUXN5322FoxWh00iM/f6FUrfThpzdMpFQR5zmqRvsTOk7XN0j5oCPvQHUG c/4Q3n3+7ik6MtiKdh+SGxbpLo+neHtPdvW10Mj8tvpoQPBAZQ5gYmp22WDwIpR6g7 blxvhafJiAGbJyKlLQQDlT7nNBjiv+7CU4yZt0s0knQnaVOQlhvF3mDOPimBFGLLaS t7IBp7CGjkkgew7SeFy12qwunaSAi2GseksFPTGCLricf1vMWtyIaehXHXGf+18vWo 7fdG2EJZPkSLg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7B50D180910 for ; Tue, 25 Jun 2024 00:26:53 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 00:26:53 +0000 (UTC) Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-52cd80e55efso5806971e87.0 for ; Mon, 24 Jun 2024 17:25:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719275135; x=1719879935; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=SDtPZg3DV3dJvirOpS8IT9fTHYwRDjAsmwqg68Df6Sg=; b=ft1Elwm3JoE5ekxTLKCWR4Y4uVeNpN48WxOLkAxxGlK47LybKJynLMYnjUQvYjTZIK gcmeEbhlUSOhSyft3Ntfvx5ejfd04QvAO1/f8j/kb6jZlvuvn7NZOtkzD/jAZT7mofQK C85RSudby0DYzcwdLQaHtQ6W49jLo43UfHYVhj1w76LqyV+leeD8Url0lkYsOzTD8p84 mCyaBwupXUBE/OZQuKsk60abVn086sMWWlkFANyRPuCziYh7A5O2lApv9c50PUSX7Sjs lwQfj4Tl7IsabB+Sw+qjrOsl5RsvZFypl6SyPJ8PSaHW35KpniF+dBN1pRzxMTMBF5iz iMWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719275135; x=1719879935; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=SDtPZg3DV3dJvirOpS8IT9fTHYwRDjAsmwqg68Df6Sg=; b=MF2KDqeDbuDG0aX4b5ePV+C7W9dDFRt8EDZGenjLV8zFsfXwdZJndgF9Io/8tlfC47 rv1Ba3FjmMLjZxoPJXyaFEGa9wVL1HhDO3zos9pC+ljVn0yodH2+esWjhqpN/W9dLfsm GzuL7MMN1rBrotcxkgqlcChD92nP2JcGDFSnSW1pC6KZECY7+P4liRXs1Qgs3VWi+bA4 y/Z1jNB2ffG1DLlChcdr6cU7a1HtIMTxT7GOH26HrDgIuaXMg0kJStoD2EF0LtreyEHd Yn3pgA90z6EB3YYjnpBxlK9kkUjOoFEhMeieAIEdq+Jl3dvbK7xAmNIy80DugwiyZBju MX3g== X-Gm-Message-State: AOJu0YySGpN9G8wHcbxyuh7f2iG5hBFCE1BIHwcRT76ksIXgKz9FzM0w QaUOa7WyJ65GKK3p8DM1TG9hajGvxxoroyMHr53UVNoHK2SQ1bArprZVo9L6kTQ0O752sQ9NKF9 QmNADufxIvNG9LAbGOpdCfNOLNDvMfIeo X-Google-Smtp-Source: AGHT+IGVyXltiRecs0rkKEVkmh6GIJKV/qI5qc4jQ6KClolvx4mDxr/cf31Gp515su00+rVSNWPqMWfyCwKFV9w7wr4= X-Received: by 2002:ac2:47e1:0:b0:52c:df8e:a367 with SMTP id 2adb3069b0e04-52ce18526e1mr4374364e87.53.1719275134398; Mon, 24 Jun 2024 17:25:34 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Date: Mon, 24 Jun 2024 18:25:26 -0600 Message-ID: Subject: [PHP-DEV] Add $this return type To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000a83b90061babef09" From: radar3301@gmail.com (radar3301) --000000000000a83b90061babef09 Content-Type: text/plain; charset="UTF-8" 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 = $name; return $this; } I propose to add "$this" as a return type, ie: public function setName(?string $name): $this { $this->name = $name; // implicit return $this; } public function setName(?string $name): $this { $this->name = $name; // this would be an (compiler?) error return $somethingOtherThanThis; } public function setName(?string $name): $this { $this->name = $name; $self = $this; // this would not be an error return $self; } public function setName(?string $name): $this { $this->name = $name; // technically useless, but not an error return $this; } public function setName(?string $name): $this { $this->name = $name; if ('foobar' === $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 an alias for "static". Regards, radar3301 --000000000000a83b90061babef09 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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 r= eturn $self;
}

public function setName(?string $nam= e): $this
{
=C2=A0 =C2=A0 $this->name =3D $name;
<= div>=C2=A0 =C2=A0 // technically useless, but not an error
=C2=A0= =C2=A0 return $this;
}

public function setName(?st= ring $name): $this
{
=C2=A0 =C2=A0 $this->name =3D $nam= e;
=C2=A0 =C2=A0 if ('foobar' =3D=3D=3D $name) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 // not an error 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 ab= ove legal examples
}

It should be obviou= s, but functions outside of a class context would not be able to use this s= yntax.

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" wo= uld be considered an alias for "static".

Regards,
radar3301
--000000000000a83b90061babef09--