Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119207 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56124 invoked from network); 23 Dec 2022 14:37:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2022 14:37:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E61461804BE for ; Fri, 23 Dec 2022 06:37:03 -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=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 23 Dec 2022 06:37:03 -0800 (PST) Received: by mail-ot1-f48.google.com with SMTP id x44-20020a05683040ac00b006707c74330eso2926734ott.10 for ; Fri, 23 Dec 2022 06:37:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=VF5I5VgwyXqmhTtGQDtto7oEFe/K57TykzZSILVo3u0=; b=PSIg7CBGLmpVM5KnHIGa55ZisGn+KIjUScyuihQcY2BM6WwzQY07Zs/g0qVEfqSdh2 FaePguTVQe2cmVmf5lDi1pGZAg8SqKegnwhMqJ9rerqmdFAjh0Qlzw2rce+WPyIvm/pc Q3MqnBet4yk1dq+1NhZLJNXMcZyffJjV73nTHbdI9rmvurM7ATfAdwf0MYA1vtzEDlnj 0TKn/wznefSPisvNO2jEeGuFyEw52UTv2mAClNeMAUNgpaTfZeeKrhqwVNveYWPpXIfH XCuimbiRdhIZgKnGeUHKZvmVc2U6q6IjOhYcZ53FVu8btXmUwnaR9s9Vh0/L9IWVdBL/ vAMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VF5I5VgwyXqmhTtGQDtto7oEFe/K57TykzZSILVo3u0=; b=gPVDGgxWipY8r08weP90Gz5bQbpwJJZyqKG8ZUhjJn8Y5AVWzU7lKE/RXExcxBrIeK BzLujDr9MGTBOUaKFs5HejDIcIBYyulmuz/f5PiwOF2eNmw6zRRXJuiaq0WXdsT+2O6N m+icK1v+dDMZ6y+FzBq1AiBDXu89btlyUGMVFlHopdZirI7DOsugM8yf/tv/4xHv6Pm7 MZcbkCy0WzIwZmNcSJyzwYiRWMKxZo5vHDPFUUWU6qCNjO3EzcyT+E+pqYDfSvPpluZB ttFrxIKCmlSAXFIN9Avt+bOMr0ur2xqoirHRcKwNKXYZljLjmvlLPucnb6pBucisS6Xn cnnw== X-Gm-Message-State: AFqh2krkUjNLJm9lBAQqFkUJCh08iGQ+xbbcBNKA20f6Pxj5dItCwd32 vaaGfQNx05YsvjWPcW+XbunEeECcyg6M7K4Wxih6Ey1pohU= X-Google-Smtp-Source: AMrXdXuk6a3Erl0lknAwvTWZauZL2tXwBEQmBLF32M1NBpb7TO7Y7uq6zLc/yTf8t0OQ/xqXx5MNAYOEqV/7NJBuZpQ= X-Received: by 2002:a9d:7dcf:0:b0:676:20e6:ce15 with SMTP id k15-20020a9d7dcf000000b0067620e6ce15mr655296otn.317.1671806222616; Fri, 23 Dec 2022 06:37:02 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:6802:321:0:0:0:0 with HTTP; Fri, 23 Dec 2022 06:37:02 -0800 (PST) In-Reply-To: References: Date: Fri, 23 Dec 2022 15:37:02 +0100 Message-ID: To: joke2k Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Methods which auto-return the class instance From: landers.robert@gmail.com (Robert Landers) On 12/23/22, joke2k wrote: > Hi folks, > > What do you think about having a method which returns the class instance > `$this` by default only IF its return type is set as `self`? > > It is very common for fluent class methods to have a verbose `return > $this;` ending in their body. > But If you have declared `self` as return type into a non-static class > method you have only two options to return: > > - the $this object > - another instance of the same class or subclass > > ... in order to avoid a return type error. > > My proposal is to set the instruction `return $this;` as optional for > non-static class methods with `self` declared as return type. > > Example: > ``` > class Counter { > > public function __construct(private int $counter =3D 0) {} > > public function hit(): self { $this->counter++; } > public function print(): self { echo $this->counter; } > > public function split(): self { return new self($this->counter); } > > } > > $main =3D (new Counter)->hit()->hit()->hit()->print() > // 3 > $sub =3D $main->split()->hit()->print() > // 4 > ``` > > Thank you for reading my proposal, > please let me know if you like it or if there are any issues with this. > > Regards, > > Daniele > As someone who used Scala for a number of years, I=E2=80=99d be very much against this idea. Scala automatically returns the last line of any function and we found it fairly difficult to maintain once the project got fairly large and ended up switching to explicit returns. Mostly we found it too easy to return the wrong thing. In all fairness, I guess you technically don=E2=80=99t have to use it if yo= u don=E2=80=99t want to (you can use explicit returns), but IMHO, using this would greatly affect readability. Every function you read that doesn=E2=80= =99t have a return, you=E2=80=99d have to check if the return type is self/stati= c and verify it is void. --=20 Robert Landers Software Engineer Utrecht NL