Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119204 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32753 invoked from network); 23 Dec 2022 11:21:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2022 11:21:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 28460180504 for ; Fri, 23 Dec 2022 03:21:02 -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=-2.1 required=5.0 tests=BAYES_00,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-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (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 03:21:01 -0800 (PST) Received: by mail-yb1-f173.google.com with SMTP id n78so4951259yba.12 for ; Fri, 23 Dec 2022 03:21:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YbjEDfQ5u6Ipxz9HSW8+U9Zh0UbRC/22OhHFtSUmg3c=; b=iuIoP78ribCbkIKL0XTar3WN3DILPz3lsyK5N7BnRJOQIH/NtDdNJlFUK5OaWKeJJj GQaEhLAbCe3PaZR943fl0tH0ABnkkXruglDZ0XkI50yi+XzN9UmyZM/8fnBmJxEIhf9J qdrvDxsvLyjvZedCe5vfqcNCbr3F+HkBJBcOCEJ928SvNUD7T8hLfUgBt3uiDqjsqpik phIgJuhEv53ZSVSyfgKHI/OQbbjtji5raXtbaKtcL/K1hJfRuKQ/gHGgMoHKsocLivCf cChp31jGHBZ5EsVe98TkS9lxuMRQgLwHmNo+I6kRnn78Wk7KkgFnyhudJLIsbEY9yU44 zg3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=YbjEDfQ5u6Ipxz9HSW8+U9Zh0UbRC/22OhHFtSUmg3c=; b=K+4P/jafjpyWVsOrTxAe4qYNFR+/yWdpZAQ5vNlCCB1XLpiHsH5LuNVN2CxjE/M7H3 0foEpFclXnutTTdB4rAxDtNWE7rum4YkHFaEvyu++Fat+kgLvjwR7opxO62BGq3J83Jb S0K+zy0fXSLMSBEHJYtg7/UQVEaRh+dw7lrGt3p26LrV+vV/G7D6e6i8zeNXv4awmBNN tVqnhPhsizllOeYgQ5OVUdz2YpLZXuQlDSQniU1j+Okm+zr6pm+ltsP+fJIBVL3rFEB1 b5isZIol1gm0UknGOGvX72nj/h6FCyF5LIyx8Evk2jYYABLaDWjnft6bMSaWc7nrmsme jO7A== X-Gm-Message-State: AFqh2kqNAj5Xbue0/rlMbwBxsmGJGbyywdtQwIqA1Y3lAEYVVZXcgsCB an5lFnXxP3+TZEZzJaZf98DSoxELjZfbsy/oMhk= X-Google-Smtp-Source: AMrXdXtt09AqwjZ4/hfm13/NPg38+r0TvwsLXeDXW/g4AHjRjmd3nPpeoJpm7XCHIdPgrN/n1LFJq6UEAXk7TaReRSE= X-Received: by 2002:a25:aab0:0:b0:6dd:9027:5825 with SMTP id t45-20020a25aab0000000b006dd90275825mr1037498ybi.155.1671794461078; Fri, 23 Dec 2022 03:21:01 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 23 Dec 2022 12:20:49 +0100 Message-ID: To: Claude Pache Cc: joke2k , internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000fd796105f07cfa39" Subject: Re: [PHP-DEV] Methods which auto-return the class instance From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=) --000000000000fd796105f07cfa39 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Daniele and Claude, pt., 23 gru 2022 o 10:33 Claude Pache napisa=C5=82= (a): > > > Le 23 d=C3=A9c. 2022 =C3=A0 01:08, joke2k a =C3=A9cr= it : > > > > Hi folks, > > > > What do you think about having a method which returns the class instanc= e > > `$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. > > 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 t= wo > options to avoid a return type error: either return the `$this` object, o= r > return another object. > > > > > 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 retur= n $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. > I like the idea of `$this` as a return type but also strongly believe that the use of `return $this;` should be forbidden, eventually for control flow use of `return;` might be justified. Cheers, Micha=C5=82 Marcin Brzuchalski --000000000000fd796105f07cfa39--