Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119205 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41871 invoked from network); 23 Dec 2022 12:46:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2022 12:46:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C8720180503 for ; Fri, 23 Dec 2022 04:46: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=-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-vs1-f50.google.com (mail-vs1-f50.google.com [209.85.217.50]) (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 04:46:37 -0800 (PST) Received: by mail-vs1-f50.google.com with SMTP id a64so934652vsc.2 for ; Fri, 23 Dec 2022 04:46:37 -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=fGUyfZJrbn419oVbWxZvpQRj/RLjbvb14B2PefHzdXs=; b=gPaVZaLYGl551J0oP9l7qP2L+zVlEr9uyYV4oOSq7bdXaMSe2tFrFn/vqIsmaWcoIe 6qTeEYTProIzSSC5Q4W23fKQte86nhlvPA4g1R+nP2EF0kSbaKcP84ssf1gXYmkVh8OY jaWqjgmcL7Uc1OyVRPe9ltsm9djRzfJh+619LDS89IRtqwMZoJuKWdFmmnfAdUpC3Aos /SiikoIjV4MVtz1uRbu8L4gEzupXSLLAFLCXAHLx2GgCvyeg+1sn2uRpdZExM00CePPj nRVDTxbkXt8qmu6iwDu24PIPwAgwshNy4XQf5xqk2Em5dwF2a8IJagTaTW9wkIh6Ddnb vswQ== 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=fGUyfZJrbn419oVbWxZvpQRj/RLjbvb14B2PefHzdXs=; b=XLVlFktWQ5HUnseCeFA+zuFIf+nvZBhtOMV0Z/Y0AhUkhirE6oxsPdIe7tK9B0igkS 1jH02B5215SgtvxXxZuCuQNoJYB+lGneZOklySWICpBdMIMiA7gjIbBFDH3pjEtMVrmy ND5dSPE00Y+GxmpCjvp4Y2ayNmNkCkXHkkf+waPl84tdAW5zJ+2cC6YaG4AyR6h8oGTL 8pICwk7zkQCfsJo4AZbSEy9hQaXSzz+jOrIjnfU+sle80dTLg59nGpJi5U/3w/vM2Kc6 Hl4pMi1vnpWKTJRyl4+4LH+hSYmNDZFavSepaj2KgU6IcO9hYLCVZS/yJBbICVSrWbPO slIw== X-Gm-Message-State: AFqh2kr5dknNwFXm8J7jfTI5ZzxGvHOOM/5knGWVzJGglOyhvIdripMP 9ikvIQeZOLmtfrOAprWpIdBKEqll+dWwoR31ZDE= X-Google-Smtp-Source: AMrXdXvVxzGOBPfwk8nleriUBd4tBPB4pE98gP49WE6o/SxMBAxpiYsVFEOmTMv5tpNsWgEeRMEITud3Ynd165PllOc= X-Received: by 2002:a05:6102:1270:b0:3c3:9c3f:b2dc with SMTP id q16-20020a056102127000b003c39c3fb2dcmr464089vsg.85.1671799596758; Fri, 23 Dec 2022 04:46:36 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 23 Dec 2022 12:46:25 +0000 Message-ID: To: Claude Pache Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000019bcd505f07e2db4" Subject: Re: [PHP-DEV] Methods which auto-return the class instance From: joke2k@gmail.com (joke2k) --00000000000019bcd505f07e2db4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks for the feedback! On Fri, 23 Dec 2022, 09:33 Claude Pache, wrote: > 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. > Yes but declaring `self` is more strict than `object`. And talking about the word itself... Declaring to return (your-)`self` but you could return someone-else of the same type as you. But it is ok, it refers to the class name not the instance. just semantic. 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. > Return `$this` is a valid alternative indeed. It is an explicit subclass of returning `self` which has a default return object, itself. --00000000000019bcd505f07e2db4--