Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119200 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66250 invoked from network); 23 Dec 2022 00:08:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2022 00:08:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4EF60180504 for ; Thu, 22 Dec 2022 16:08:44 -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.6 required=5.0 tests=BAYES_50,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-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) (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 ; Thu, 22 Dec 2022 16:08:43 -0800 (PST) Received: by mail-vs1-f49.google.com with SMTP id k185so3223206vsc.2 for ; Thu, 22 Dec 2022 16:08:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=hsNOtQFnCfX/7elb+A29vwDeELVNdRsFwmIpdhtp6Cg=; b=m0w97Uk9Gn9GO7gAr7cEv7oVhEEMXjgB5Mm39yDRCEv+iyShyWvqKDlBvN5X+LNnCQ d8I6NU2xUNzGXC7kkZ3c4JqDhc11ExlHxedL/YNLbgcxowUyPpYze9h6o4Om/V+L9s43 Mg1Fbywo6kQev1ez1HfDJSlJerA64ePA7HskULLSXqtONfcEpa4/yroL55uxCF/6MW1W G8kr4VkvsxeN0IQ4BHHSQ6HlE4HmBQi7v7JRb9s7iQjabqy4Fx++1sc0LaCH1OCWkaaR U2vwyZ4+NlA/PRVXLk6DzMGuosjG5ekaeFBMDdau851qO+5k2oHIpMWzxaIsvaXZ27Co 8ygQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hsNOtQFnCfX/7elb+A29vwDeELVNdRsFwmIpdhtp6Cg=; b=KlsX6at9hWd5+mUjEPWp5zyzIVX9sTstvIeWdprLNcMA6T7hnYLps7XtuR7YKiXXbz C8k8MjvHO0MDX4rVHrjHgwymOvf9hBgqu3Bync+3/l+HiClDND61naHpriA5NDJB9CkB xEyXG15GMAfGRvYDVV9MG++MIw7d1ZYKlaubWSYUJhLZBXCt/MiGgNJhKcOn4A40DPfB wFybBJ/JW3amDMLK1fS+JLk600/zd+IeH+pGuLRqpCg6PM12dKME6E2YBx4iTZQj/6TV 5sdqldbzUasFWgWRUPNs2Rx36/L3D6Uv/CiJ66qIM2kfO5BcBRE70OsR7h2QVKrRHx0l OVZw== X-Gm-Message-State: AFqh2krzKLhaOppnJctoQmNMqMS7p0k+WcVG4bm4JeHBJ02C1GswtyBs 4t+bJUYKc5CO6l8icx6bYSZbwI5FWI1mhQwKgUwUCMvdEUysOw== X-Google-Smtp-Source: AMrXdXvlBbyq2ZzwDzIjlPDnwAGuoiy0z6cXXqRIxEU3TmN4pRxsD6MhOvzpzig3p+/mnaLxIgWSoaWlufMdJUy67nk= X-Received: by 2002:a67:c011:0:b0:3bc:7750:6ba2 with SMTP id v17-20020a67c011000000b003bc77506ba2mr808955vsi.39.1671754123225; Thu, 22 Dec 2022 16:08:43 -0800 (PST) MIME-Version: 1.0 Date: Fri, 23 Dec 2022 00:08:32 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000aaaeac05f073968d" Subject: Methods which auto-return the class instance From: joke2k@gmail.com (joke2k) --000000000000aaaeac05f073968d Content-Type: text/plain; charset="UTF-8" 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 = 0) {} public function hit(): self { $this->counter++; } public function print(): self { echo $this->counter; } public function split(): self { return new self($this->counter); } } $main = (new Counter)->hit()->hit()->hit()->print() // 3 $sub = $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 --000000000000aaaeac05f073968d--