Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129086 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id B34AE1A00BC for ; Wed, 5 Nov 2025 09:25:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762334734; bh=l1LRmr+e0d0HhfbxunnbeQot2bk5kYwX+OjrJfpjV94=; h=From:Subject:Date:References:Cc:In-Reply-To:To:From; b=UyEC/XeF3E8Cx6U4R8aZ8Mx9QSms1qfIv4iDGVGpBhqAPMTXDaxJrSvNM4XUK2GAg DdjPEIFAUMnMsns61LHhlC+pPCF3kcOc+/bTBEGSx9OdMn1V87pgPnx3Cssd0cL9Rs SmfePh6OMK0fE5I3gu16E0qsuKdqzmZmey3S7oy3RLPG2rjKEmIm9lPJ0+AN2ek/8K Fmnwpe3eTMyAre0/rmY/26G9dgRFaZa2JFqSkuxlvv4Qw89Ikv3L79iSOsnfhCEZiY iyiUgBX/We/0aDbUeS1ESmt+2B0Oz15ShSFqNLUGK7D7JSXzC2G8qELiD2hn8VHoKh t4hPfO1She8kg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1AF8118002E for ; Wed, 5 Nov 2025 09:25:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: **** X-Spam-Status: No, score=4.7 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_NONE,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 5 Nov 2025 09:25:32 +0000 (UTC) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7ad1cd0db3bso1389953b3a.1 for ; Wed, 05 Nov 2025 01:25:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daveyshafik-com.20230601.gappssmtp.com; s=20230601; t=1762334727; x=1762939527; darn=lists.php.net; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=7SNzNLMadPDz8TJhixbR3C1QzCVxg2eu4B6zYwiDA0o=; b=RMgam4ve99amj5kcj7IpYBKjNCHiL09nDCg+MfbmVMez4mDVUx07UMcfjKa6TobztR MwnEcuEiL0f7fXD896ldlIdaDBO9HXt/5pXgO6kKADy03cBsHNTPwMQzquOsJ8QpZBRC T9ezqM5a5+rY2zVsL+N98RR+mk6AR9JjcF3vuYwGt2ZCVN6jqbY+SFXX6DQ1TAcELfX1 Y16NAbITQx8rG4M/GqIOBsdSuQa67odilEeg1v0nQ44tNGpN1w8OKIIZ892sPt3unpSX fZ5NeWR+T7DO/RuGRUXALpJbKc4Y8FoNAUvWW5WU7NqAZ7jAdSAKb6nmYp/rxgTY7WHi yUwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762334727; x=1762939527; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7SNzNLMadPDz8TJhixbR3C1QzCVxg2eu4B6zYwiDA0o=; b=HaTpnceVXzsITjopobiJg7uPuxtbgMy+RubQE9U9+OYvm+vPMOvqLuzMnqSIgb6M51 RCJMXHmcKhAovTQZatM8N/UjZIkzzfrMkfPPupBPDnfO5N86iqCK+n6NAcXZKwnGFXKk rDIZvhB9aN9Wh6dFy7BP/8DG/jRAcZrpACixp98inOtc4RfqazQNQbhxdq7EfqGMFZ5F NNkQbiMyySyY+68QYYbhr0ChJhrt3aJJ8bYE/K/7RftE51dNH/zo5GFSuRePdzhXd24W B2g9uWmojVGFbA50ydHoccud9A6WocEZVWsn36y9bY//Jk+zL5M4QpXgGgNNtlLOhyeJ Jo2w== X-Gm-Message-State: AOJu0YzYc4xkjXWdqq85fjvBwlwYdq1z9GUNRa1+600nhEW5vGzdYYtD 8MLFSk75/RX6XimjV5AchSPSpG6Z1iSRoQx/gUdeFB7ILFKIGvNuswOA86EiQBX9xREa/I1nnRp ssgJt X-Gm-Gg: ASbGncuiSr3vz+gy2e9D68DiOqP1HlKj0PADEFXtfeyr/3OGlfiO7JIJg2Tm/10C2vr 20uDjDXdNYX0I80cmPTQHd3WDFcpvohKDkCc5LDXe8NRntgwwH/72v4eTWz2j7rSf4u4qwuZIHF +tcXL6el+T8fE056ZSIaZMKpgHoAMF37Ghl2sK8EuxhSkzBicHZLlDkPaheh/sKufT1Y8fR5IBk ZWZi96S2WBo0P2gKOWG/3YuxpMihrc6cSGP1UWpFrYuXTfcUTvV9D7y3RX243B0p1zeR6ILNaV9 XRy1MpxU0KhSsDJ0ZhSOS2uvu7Vv4ZM/b4lkxHyKn117S0AmOG0AcqealwEYKnwSX+edqbiKcaX mio7bFBnG0Oozd+KOKY/mlhBlOQGVPOlr/TspwmKUjl8H7ctT9kBprw0zQCCgoRe2E9YrVZ062e SSLP1Vd65YFc+lEUa09ebjuGsVdcXA1leJ8BXxSerQXMBxNYmgIHMjC3p49sC+ X-Google-Smtp-Source: AGHT+IHDvgHB6VdY83SSEiZvBo0/aOSgIwjfhhyekcncw1elbT10iOeVRl4VuhQfmirAOCgf5BJUpw== X-Received: by 2002:a05:6a00:14d4:b0:7aa:8397:7750 with SMTP id d2e1a72fcca58-7ae1cc6115cmr3669508b3a.12.1762334726623; Wed, 05 Nov 2025 01:25:26 -0800 (PST) Received: from smtpclient.apple (c-71-237-217-234.hsd1.or.comcast.net. [71.237.217.234]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7acd5774c21sm5658397b3a.43.2025.11.05.01.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Nov 2025 01:25:25 -0800 (PST) Content-Type: multipart/alternative; boundary=Apple-Mail-4CA3F072-C252-4512-B541-F2AC2B638BDE Content-Transfer-Encoding: 7bit Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow Mime-Version: 1.0 (1.0) Subject: Re: [PHP-DEV] [RFC] Context Managers Date: Wed, 5 Nov 2025 01:25:14 -0800 Message-ID: <066080FD-0190-4067-94BB-E25D538167DF@daveyshafik.com> References: Cc: php internals In-Reply-To: To: Larry Garfield X-Mailer: iPhone Mail (23A355) From: me@daveyshafik.com (Davey Shafik) --Apple-Mail-4CA3F072-C252-4512-B541-F2AC2B638BDE Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > On Nov 4, 2025, at 12:18, Larry Garfield wrote: >=20 > =EF=BB=BFArnaud and I would like to present another RFC for consideration:= Context Managers. >=20 > https://wiki.php.net/rfc/context-managers >=20 > You'll probably note that is very similar to the recent proposal from Tim a= nd Seifeddine. Both proposals grew out of casual discussion several months a= go; I don't believe either team was aware that the other was also actively w= orking on such a proposal, so we now have two. C'est la vie. :-) >=20 > Naturally, Arnaud and I feel that our approach is the better one. In part= icular, as Arnaud noted in an earlier reply, __destruct() is unreliable if t= iming matters. It also does not allow differentiating between a success or f= ailure exit condition, which for many use cases is absolutely mandatory (as s= hown in the examples in the context manager RFC). >=20 > The Context Manager proposal is a near direct port of Python's approach, w= hich is generally very well thought-out. However, there are a few open ques= tions as listed in the RFC that we are seeking feedback on. >=20 > Discuss. :-) Larry, Arnaud, I really like this RFC but have a couple of things to discuss: - automatically re-throwing exceptions: I think that this behavior, especial= ly with a boolean return value deciding if it happens or not is not intuitiv= e. I think a better approach is to do nothing with the exception and let the= user re-throw it if desired. I can't think of anywhere else we re-throw exc= eptions unless the user indicates otherwise. I'd rather leave the return val= ue for return values; we could expand this allow access to the return value l= ike: with (foo() as $foo return $bar) { }, and $bar would be set to null on v= oid returns. - context variable and scope: I know that you explicitly are not creating a= new scope, this means that the context variable will clash with the enclosi= ng scope namespace, and then the variable will be unset after the context en= ds, this doesn't sit so well with me. I think I'd rather see the same behavi= or as arrow function arguments, where it does not override variables of the s= ame name in the enclosing scope and whatever value it has is lost at the end= of the context, leaving the outer scope version intact. At worst though, I'm sure IDE and static analyzers will be able to detect th= e "use after unset" behavior with clashing variable names, causing the devel= oper to resolve it, and it'll be fine either way. Thanks for the great RFC! - Davey=20 --Apple-Mail-4CA3F072-C252-4512-B541-F2AC2B638BDE Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

<= div dir=3D"ltr">
On Nov 4, 2025, at 12:18, Larry Ga= rfield <larry@garfieldtech.com> wrote:

=EF=BB=BFArnaud and I would like= to present another RFC for consideration: Context Managers.

https://wiki.php.net/rfc/context-managers
=
You'll probably note that is very similar to the recent pro= posal from Tim and Seifeddine.  Both proposals grew out of casual discu= ssion several months ago; I don't believe either team was aware that the oth= er was also actively working on such a proposal, so we now have two.  C= 'est la vie. :-)

Naturally, Arnaud and I fe= el that our approach is the better one.  In particular, as Arnaud noted= in an earlier reply, __destruct() is unreliable if timing matters.  It= also does not allow differentiating between a success or failure exit condi= tion, which for many use cases is absolutely mandatory (as shown in the exam= ples in the context manager RFC).

The Conte= xt Manager proposal is a near direct port of Python's approach, which is gen= erally very well thought-out.  However, there are a few open questions a= s listed in the RFC that we are seeking feedback on.
=
Discuss. :-)

Larry, Arnaud,<= /div>

I really like this RFC but have a couple of things t= o discuss:

- automatically re-throwing exceptions: I= think that this behavior, especially with a boolean return value deciding i= f it happens or not is not intuitive. I think a better approach is to do not= hing with the exception and let the user re-throw it if desired. I can't thi= nk of anywhere else we re-throw exceptions unless the user indicates otherwi= se. I'd rather leave the return value for return values; we could expand thi= s allow access to the return value like: with (foo() as $foo return $bar) { }= , and $bar would be set to null on void returns.

- &= nbsp;context variable and scope: I know that you explicitly are not creating= a new scope, this means that the context variable will clash with the enclo= sing scope namespace, and then the variable will be unset after the context e= nds, this doesn't sit so well with me. I think I'd rather see the same behav= ior as arrow function arguments, where it does not override variables of the= same name in the enclosing scope and whatever value it has is lost at the e= nd of the context, leaving the outer scope version intact.

At worst though, I'm sure IDE and static analyzers will be able to d= etect the "use after unset" behavior with clashing variable names, causing t= he developer to resolve it, and it'll be fine either way.

Thanks for the great RFC!

- Davey 


= --Apple-Mail-4CA3F072-C252-4512-B541-F2AC2B638BDE--