Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129187 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 CD7E51A00BC for ; Mon, 10 Nov 2025 07:13:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762758839; bh=l6RJihpp4TojhH9ISnSavppQjMHeOyxzSjXaI0i4r50=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=QF5C9Y/MwzfD414jAZrq3EeXn+XIkAPG+Nc1BLhWuRWAQ2dtReSoVsCWbwno+0vGu hYbFutI+C5Vy74PRDeK1wxOYa4Tp+PC4sBXhA5lxcQQtZfrtakF7pZbyZwJMRzlHWz O6IA9/saa9K0IrTXSjRaXTXzDFjQyJ8HXppzAhQ24prGFx0lVflH/+Zx9Ud5uCN6u8 z9eBGoXCLfs4EKqOp4EuZcyoIrgdj0B6TFiArm/4a8wfEgyGNEQ/sMGGwD7pvNyZgF WtAWrZm8OinjUb6QYQSpZ36EeutHmjDMMuLe1eqrv5zslHYWy43Xmz6I3Hpwkh5ZgB t9QIYTTczP+WQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2639D180055 for ; Mon, 10 Nov 2025 07:13:55 +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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,T_SPF_TEMPERROR autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f48.google.com (mail-ua1-f48.google.com [209.85.222.48]) (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 ; Mon, 10 Nov 2025 07:13:44 +0000 (UTC) Received: by mail-ua1-f48.google.com with SMTP id a1e0cc1a2514c-93721d29eb8so1374087241.1 for ; Sun, 09 Nov 2025 23:13:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762758819; x=1763363619; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OeN+KyqmuS742ifwlhSqkUfO2Q2TMgt6kwsc470T8OU=; b=UYxnpzsjgyEUPW506fObbfEIl4iboTAGeCpem9CjS2OxLNf8dH/e3mx2pVMAAQ4C8f JsyCX/TJkG72cGI+Fd+TYw1NVLLQhsbLXSzzxcJfgXpjcmXqVMQ7uFVvPZ3pSb0FXTDB laQtjMLzSDDrgvxct3YxnUH9wkxInRKOnH792ExDeV8SY+n0IWYQps0ZLML8cBOQGgWY ubAsOzflO3b9PB+/QLG4yWeXgdO3NoWMbkHsntdUc8PIXywZNHFknFqlob02+85W11Ao TWaS5GOcXYwXjBDdUa8xcyOHDbgKtwe43meV4kfdqYva/6yjPb8Ztb4Pokf/AItWllLV rq8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762758819; x=1763363619; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=OeN+KyqmuS742ifwlhSqkUfO2Q2TMgt6kwsc470T8OU=; b=ngKEYhEv0OIaoF4wqeeJL8ujYMrGW9+M2g9l3RRqS71u0M6pNaYJL67nRa2jbmoIsZ 2+gki+T1l5fiZcxVbgtO0kKkM5WriRG+up1YGctyjTMv2tCKP2KEAYEqA3LwyPdjPby5 W/aOhh/M9qs4gEBPPQBX2lBAm3QpIi5JHRGrAwAjU8QW3oa1obQEIoHVcQss73vPylzg rBtCWOpnfp6XE/xexW3RmAsr3q9g9iarS1lBojVvncmizf8jiUaaWHp5+PgdTqOeUIHD Jq6ZvsbT0TR9UB921vHxrFL+4VXeH29UHdQDXxXgLHBeCjx+k56LbjI8d9jB2ezLsLVv KUbg== X-Forwarded-Encrypted: i=1; AJvYcCVCiUg+e97Y7Jk48JF/e4n++rE9EmBIbtRUt/iXWAcrLwelOiMONeJamxLjdb+/Z2hipK7YPru3MSU=@lists.php.net X-Gm-Message-State: AOJu0YzPXifIN52PWYFC/sEHQ5OurNoE62YpQztCNyV/DP/C9qpfPP/n W/0Gzl5OkAjtgNi02ai2Br5Ro1ZjTMszkPuSN9Mo06uQSN0OGPLDyAJQBbKS57fw9JDgwgxCyZy 3VlQJEiBlwYFZJiToL2TzNePXd8kmrno= X-Gm-Gg: ASbGnct94kXt/hwWXLHv/aefHqlGgCmP1j4kbhuztq+nH9hIymbfgFsphNrus7kaML2 awYmzkySYMTSxY1uHPjnoZsDac7Z/Nt7f1IRDg0NojBjojR1BJzcWyBjpdRjHrt31sp2YMpJssA qRrzHP2R5EwzCr2g7GZ3CnFWLIMFcjtQa+kT1UcJbYlqH2xrFBfDLl1Bt+UJgw3mKBFODYJD0Io oHrVeJcnIhQcVZrnDr01WlatdtQrUr91r4q83DVFbPCp+qF4R6wJfKk+7wn11ahScXTzkhkf+nr EB0rM+OZBjSzRtrRnqt1+y29UhA= X-Google-Smtp-Source: AGHT+IEtuXKM12nRl2z0/ZhNUsImvagvPncFzhlB22Pru6PqOwvkxsFEILXdnw820SNb9LmkvaH9EBhOQXPlyEjk0bs= X-Received: by 2002:a05:6102:d89:b0:4e5:8d09:7b12 with SMTP id ada2fe7eead31-5ddc46dd0eemr2443501137.7.1762758818792; Sun, 09 Nov 2025 23:13:38 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 10 Nov 2025 09:13:27 +0200 X-Gm-Features: AWmQ_blpmTr7n0Hzb0Lx7cfY8V7-vHWB3upnZk1eSsCmRYrGWRzK2I-9x6_LByk Message-ID: Subject: Re: [PHP-DEV] [RFC] Context Managers To: Bob Weinand Cc: Larry Garfield , php internals , =?UTF-8?Q?Tim_D=C3=BCsterhus?= , Arnaud Le Blanc , Seifeddine Gmati Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: edmond.ht@gmail.com (Edmond Dantes) Hello all. > It makes it very easy to not call the exitContext() method when calling > enterContext() manually Consider the code below: ```php class FileContext { private $handle; public function __construct(private string $filename, private string $mode) {} public function __enter() { $this->handle =3D fopen($this->filename, $this->mode); return $this->handle; } public function __exit(?Throwable $e =3D null) { if ($this->handle) { fclose($this->handle); } } } $ctx =3D new FileContext('example.txt', 'w'); $f =3D $ctx->__enter(); try { fwrite($f, "Hello world"); } finally { $ctx->__exit(); } ``` Question: what is the probability of making a mistake in this code? What is the likelihood that a programmer will forget to call enter and exit= ? ```php ... with (new FileContext('example.txt', 'w')) as $f { fwrite($f, "Hello world"); } ``` In this case, the probability of forgetting to call enter or exit is zero, since the language now supports this paradigm at the syntax level. Question: what is the probability of accidentally calling a method instead of not calling it? ```php ... with (new FileContext('example.txt', 'w')) as $f { $f->__enter(); // Error! fwrite($f, "Hello world"); } ``` I believe that finding a clear mathematical proof in this case is impossibl= e. But if we look at studies on error statistics, it is the absence of a call that is the most common problem. (https://tomgu1991.github.io/assets/files/19compsac.pdf) > Thus, we have a big pitfall, wherein APIs may expect enterContext() and e= xitContext() Is it really a big pitfall? If so, then functions like `fopen` and `fclose` should be removed from the language altogether, because their existence is an even bigger pitfall. > Ultimately you definitely will need the capability of calling enterContex= t() and exitContext() manually too (i.e. restricting that is not realistic = either) Of course, they can be restricted: make the methods private, so that only PHP itself can call them, and they cannot be accidentally called anywhere else outside the class. But I don't see any convincing reasons for doing that. > The proposed approach only allows nesting contexts, but not interleaving = them. Why does this need to be done at all? I don't know what "interleaving contexts" means in practice. But even if they do exist, that goes beyond the scope of the current propos= al. Nevertheless, it is worth noting that studying Python bugs for this RFC is a good thing: https://bugs.python.org/issue29988 - with statements are not ensuring that __exit__ is called if __enter__ succeeds. Also: https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager Total: **Advantages of `with`:** * better semantics than try=E2=80=93catch * less control-flow code It would be strange to demand that `with` be safer than the rest of PHP cod= e. That=E2=80=99s an odd requirement. Of course, if PHP were a compiled langua= ge, many checks could be done at compile time. But we have static analysis for that. --- Best regards, Ed