Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119206 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47516 invoked from network); 23 Dec 2022 13:29:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2022 13:29:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AC3071804A9 for ; Fri, 23 Dec 2022 05:29:10 -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.7 required=5.0 tests=BAYES_05,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-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 05:29:10 -0800 (PST) Received: by mail-ej1-f44.google.com with SMTP id t17so12099430eju.1 for ; Fri, 23 Dec 2022 05:29:10 -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=UxlpnBfGYWY20vrCnevZjXWXvaLxlDU/u/hBNEwMAE8=; b=A6RxL0ugdO0+gctIcodBKd/MzmQpjNyaUHd31LLMpWq8cPQlk8Xuap0xEM7052uIAS 3EKZgf3UhKf4qm4GaCM6JgSr9VxIShzrbu5tuMm08ktgj5CdADnCe0tRY5tje4B500ez oA0Z31UMxvrwKHQaJ/yK14Vc1xkcha5K7rhHRXUXtWgQ3/bSsqPAcrSheFtoXdZ2ga8w MKAqQJw2UBXQcBXEuR2d36DEqZko3nPqe+qr7pyPNtSbJTyNWNlNHeLXpJ9m4M58Jx/k UrofoG1vXz4PUz8uM8ELkRiwPs5RufpHEJ4y1ToS7bIRoIt0Ln/5DpJAcjbbhYl+/coI j5Dw== 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=UxlpnBfGYWY20vrCnevZjXWXvaLxlDU/u/hBNEwMAE8=; b=xgi9kEbrv5/iRFLeWYGK5RooGfx+NhWVZt539Hs3X96iEZUFnarzoyYJ5CKZPa6A8W KhSU5ndUQ2HwqhjarblQBCdFr69nmhHXyH4vU6V0HkwIvEYJPB2zua96kBB0Flj6WxiC 4fLqrnGp77U8AgH18yujid0a2Ea+pONVJRRpQoD6Aesb+eqe1CZamF/+tsUgkjR2oiUz 3suBeBHcj2ZX7NgmRi5UakJ34tLIuE4AxZwvvW1szWUvyTw5UZ5HTd5dTCukjrZMJYU7 AM72umIZy/ez+03pamFAT4XYXKxTx5aphkIciIEFNicxlZlQG4JVk59fcYgl78rGFSMs zqrg== X-Gm-Message-State: AFqh2koHP+7PnX9i2VS8HinMEjwf+6xrqJrL7uHX8s0Oh1hyYC4cpwLr uAnmbDVrWzMK3hUSlCERvbo= X-Google-Smtp-Source: AMrXdXuyuX2mtOZk3usqBr78uPGQSJKSQgqQt6BEUwbbs0bgJmlp4lEYSQVxHN8F4oFcRvfWBDyDaQ== X-Received: by 2002:a17:907:d387:b0:7c1:6fd3:1ef3 with SMTP id vh7-20020a170907d38700b007c16fd31ef3mr7677949ejc.33.1671802148886; Fri, 23 Dec 2022 05:29:08 -0800 (PST) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id kw7-20020a170907770700b007c0d64c1886sm1404526ejc.33.2022.12.23.05.29.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Dec 2022 05:29:08 -0800 (PST) Message-ID: <2D4EE8E5-8BFA-40CB-953C-1E2355471E96@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_3AE92170-9364-4DC9-8E90-FEB188F54F6F" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Date: Fri, 23 Dec 2022 14:29:07 +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=_3AE92170-9364-4DC9-8E90-FEB188F54F6F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 23 d=C3=A9c. 2022 =C3=A0 13:46, joke2k a =C3=A9cri= t : >=20 > 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: >>=20 >> - the $this object >> - another instance of the same class or subclass >>=20 >> ... in order to avoid a return type error. >=20 > 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 > Yes but declaring `self` is more strict than `object`.=20 > And talking about the word itself... Declaring to return (your-)`self` = but you could return someone-else of the same type as you.=20 > But it is ok, it refers to the class name not the instance. just = semantic. >=20 It is not about being less or more restricted, it is about being plainly = ambiguous. ```php class Point { =20 public function __construct( protected float $x , protected float $y ) { } =20 public function withY(float $y): static { $newPoint =3D clone $this; $newPoint->y =3D $y; // oops, forgot =E2=80=9Creturn $newPoint;=E2=80=9D } } ``` With implicit behaviours (at least when there is more than one option), = such sort of bugs become less apparent at runtime. Therefore, implicit = behaviour ought to be avoided if possible. =E2=80=94Claude --Apple-Mail=_3AE92170-9364-4DC9-8E90-FEB188F54F6F--