Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120058 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 90312 invoked from network); 17 Apr 2023 21:01:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Apr 2023 21:01:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7FA431804C6 for ; Mon, 17 Apr 2023 14:01:00 -0700 (PDT) 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_H3,RCVD_IN_MSPIKE_WL,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-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 ; Mon, 17 Apr 2023 14:00:59 -0700 (PDT) Received: by mail-pl1-f174.google.com with SMTP id kh6so25793417plb.0 for ; Mon, 17 Apr 2023 14:00:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681765259; x=1684357259; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=9GBaAuN0ZSMyXpl9toOxmNfea4/5lfO6TY6MJqJjTJQ=; b=ieN1tGVMBBwbeIiOz9/mm0vMZx7sW67KBDMXUQX6/qwONql94aM7iAz1h63aKM4rf0 9H7/RuJJ0qXzks0Kt/MFJZdjLR39MIxaXAq9VhQ8Zt98H2ZhCgDqIv/8CxUJZu10ldBT 9TzcXM242Rxfi0PG+20nWCMNde6seEuTJV/UdhACTU6NZNmsh85ib5IM73f4op0cDAoC pdXUfAaleXxG8wylCw2GKda5g0bkEx1DKRt6lOSkXm52g39RZg4OX7YzGutLL3BW+V0u LCrcTqn6mp3ZrN4a4r2mNZFUlttKj+QqttF6dHNjqgavkhSI6+SGg/irHEc9XMJG86P7 ntWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681765259; x=1684357259; 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=9GBaAuN0ZSMyXpl9toOxmNfea4/5lfO6TY6MJqJjTJQ=; b=itvn0d2dMTgJNEget6Pw8/9hQno/JBUkogrN0U9Nyfg0YHHsGGyRMozqRifv5xxV2H WZyE7vjrSF5u3Bn+1flA4imL1hmi/SoBwRq6QPzMbDTIjFpLiO836pG2RbWgBZ/gRk/t M1MScjloOtQ4n3RsfedlcgjomudHE9uWew2wJlp7X7Gi6DtAI6E61o49xMWLjMuNqIc4 w4kD7ogErJAcXgXoHW1KKqdPUvV0Gciwt5F8nlsS4nRTJOD2J/jWONkM9GzJ4oTa8N6J cdo3RxPMoPO0T+okqFus3mpjUCwUdJ7imCMRGUqlvrfiaplg/l4GkwG8j45FEYiUpxUO wbqg== X-Gm-Message-State: AAQBX9cTnXY8plg+abpzQGqoZzH11oKZswe14r8TWzN+eE3N+0L7jnbs iHRQbsqq9cTBP2ld1jsRFqtr7/uxy605YWt1tSMJQ/X5GEwsGFZ2 X-Google-Smtp-Source: AKy350ZUb8yhVlTsYoRs/N8V5bdw8dck/1mnkXGHpwCeGG5iphG2gjAd2SqQoEWF8umKZJs3oGqNvG9IE9OksctPAnA= X-Received: by 2002:a17:90b:4f45:b0:247:5bb4:f441 with SMTP id pj5-20020a17090b4f4500b002475bb4f441mr8678975pjb.31.1681765258649; Mon, 17 Apr 2023 14:00:58 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 18 Apr 2023 00:00:39 +0300 Message-ID: To: =?UTF-8?Q?Zolt=C3=A1n_Fekete?= Cc: =?UTF-8?B?TcOhdMOpIEtvY3Npcw==?= , PHP Internals List Content-Type: multipart/alternative; boundary="000000000000d6655205f98e7ce4" Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --000000000000d6655205f98e7ce4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Zolt=C3=A1n, On Mon, Apr 17, 2023 at 11:13=E2=80=AFPM Zolt=C3=A1n Fekete wrote: > Hey, > > public function withStatus($code, $reasonPhrase =3D ''): Response > > { > > return clone $this { > > $this->statusCode =3D $code; > > $this->reasonPhrase =3D $reasonPhrase; > > }; > > } > > How to refer to any of the properties of the current instance? Let's say > this: > > ``` > class Foo { > protected int $code; > protected string $message; > public function withStatus($code, $message): Foo { > return clone $this { > $this->code =3D $code; // so far so good > // Which $this->message is what? > $this->message =3D "cloned: (" . $this->message . ")" . $mess= age; > } > } > } > ``` Yes, it's true that it looks weird that $this is present in two places with two different meanings, but that's just because we are cloning $this. If we were cloning $object, it would be clearer. $this inside the braces is referring to the new object after it was cloned, after __clone() was executed, if defined. The old instance data is already copied over, no real need to reference that. Maybe we can think about it a bit more and improve the idea. I'm thinking we can have a closure/callable or an instance method reference instead of a block of code. Also, that might be preferable, as there is a natural way to either use closure with "use" or pass parameters. I don't like too much how $code and $message are referenced inside the block of code... maybe it's not an issue. Also, I'm not really sure how easy it would be to model this behavior, to have a block of code where $this would reference something else than outside of it. Regards, Alex --000000000000d6655205f98e7ce4--