Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119202 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21025 invoked from network); 23 Dec 2022 09:33:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2022 09:33:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DF9A01804A9 for ; Fri, 23 Dec 2022 01:33:37 -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_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.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 01:33:37 -0800 (PST) Received: by mail-ej1-f48.google.com with SMTP id t17so10855518eju.1 for ; Fri, 23 Dec 2022 01:33:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=jUjK5UbQuvAWfwScoya7YybswTWzTGtnXZL/C6jBL18=; b=EZei+BzL+ruz9JMp8wUYCILKVSyy3+I3BtY3H/NX1HK6dBlgLfzjcwHivhObjnMjlD ferPMuxHxSypNOrkbqamj5mdikENZ/UoYQFpkko37dvkcaNB+nLQ2ZbcDb7a/6y2iCGK SkeLycjYaLmYjvBMkcPAWOEOA/6pzCfevXABPompzq+gfS9G0OvrnHFoCnscDnZ1CYbj URPVSmHBIycsRZIpdC30tUq3pDToCIoW1BOdBO9+rJH2KMK4rTodsSp5UbulVxcrrzUx dpGfjcV6CoEFIdYkKhWbn62RRIVWo4w7UtoNSIfEeQtijWpSKrX0TH6FKUTmg3Np0kzc dNsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jUjK5UbQuvAWfwScoya7YybswTWzTGtnXZL/C6jBL18=; b=z5DCvkrKxyeYOb6k5jcqbfyIT3jNhSEfBPzOkRS+iaAfn8f8SNjFK3a1YG31wnSg2p 8WaDtp4ur8j5JPUc7YSk5FDje/I/gEJ9Mg+OYnRSoez+zqf22NniwKOLGFpvC3AIhhjR 6jwRrXhpk5SHqXZ0+eloGz8EAkUXPS6L4ZhWyZGX9pEKTSlKglPsnKHqXGtNLEUa7rGq QCgNClpVvY8S6J2LIzzfdZG7HFpfydWr3EAZrDBbDtUVZx9ZF24bVS2n6tw3eDjK/Sq7 CdIvPSNDGeO93kDMOoJW86DJTTn5wm7UfsdS1B4ED7SyTWxrSXt7agcYOAtea4sFTyJ7 CcBg== X-Gm-Message-State: AFqh2kqby3QNfaiEjjYP143MtUkyuLMvp9Rj4N0utWREXEZfAjE4j7kl sTCq10kif8RWQI2C5yUxU1dmNzhrdJk= X-Google-Smtp-Source: AMrXdXuSqj0E8MCo+tpccUDyKYZFMoVS/9n8QygcoaUDH5gvUEqI2bd0v8l0dVTf0A5VFwQiCgGb1g== X-Received: by 2002:a17:906:b7c4:b0:7c0:dac7:36dc with SMTP id fy4-20020a170906b7c400b007c0dac736dcmr10863483ejb.46.1671788016163; Fri, 23 Dec 2022 01:33:36 -0800 (PST) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id y17-20020a170906071100b0081bfd407ad0sm1174298ejb.135.2022.12.23.01.33.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Dec 2022 01:33:35 -0800 (PST) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_0219524C-DA2B-468A-AF58-4F2101BF92AE" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Date: Fri, 23 Dec 2022 10:33:34 +0100 In-Reply-To: Cc: internals@lists.php.net To: joke2k References: X-Mailer: Apple Mail (2.3696.120.41.1.1) Subject: Re: [PHP-DEV] Methods which auto-return the class instance From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_0219524C-DA2B-468A-AF58-4F2101BF92AE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 23 d=C3=A9c. 2022 =C3=A0 01:08, joke2k a =C3=A9cri= t : >=20 > Hi folks, >=20 > 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`? >=20 > 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: >=20 > - the $this object > - another instance of the same class or subclass >=20 > ... in order to avoid a return type error. It is still two options, and it is not clear in general which one to = pick. You could also say that, if you have `object` as return type, = there are two options to avoid a return type error: either return the = `$this` object, or return another object. >=20 > My proposal is to set the instruction `return $this;` as optional for > non-static class methods with `self` declared as return type. I=E2=80=99d rather have a syntax saying explicitly that you want to = return $this, rather than letting the interpreter guess what you meant. = The obvious one is: ```php public function hit(): $this { $this->counter++; } ``` Here, there is only one possible return value, and therefore the = `return` instruction might be reasonably omitted. The real question is: Is it worth to add special cases for some specific = return values? =E2=80=94Claude= --Apple-Mail=_0219524C-DA2B-468A-AF58-4F2101BF92AE--