Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129139 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 EAF1F1A00BC for ; Fri, 7 Nov 2025 19:36:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762544194; bh=YaSi4OXJIbtBwq8FABPWj4F+XsmSDfdwP3qrpzRVxxU=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=ICODVW5MUv90hFejuCcH6AWBjLEhzdD2YswbxhtmW5X19HSDFj5sslQC5X1M/GjMH KvXiwxJEZEwwE/v+a7Ejm7Msea6z4HlmjOWCz43JwcltCzDQUg9c/yVm44BufWmYcS H6msfipn2RHOnonfqw6WA5PUr7NZovY1ZRW6+9MhAq7r8rR4+yuZpi9XgNDGbX5wUx kkUYGxIFluYnDBTfcnXtU/Y3+hQ0Tuv/Zw9zdC55tzyUWoTGBac8eckE5JKP8IGJj/ cCKXHfJyd/LDY8AVYkrNqEUKgZ/4u0d7l7o5VxAvK0UXp27FvyxddkqwplOeLmR0ut SSd/e9bmUi3Kg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8B69E1801DA for ; Fri, 7 Nov 2025 19:36: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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 ; Fri, 7 Nov 2025 19:36:33 +0000 (UTC) Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-429bcddad32so797157f8f.3 for ; Fri, 07 Nov 2025 11:36:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762544187; x=1763148987; darn=lists.php.net; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=6OoBSCQScNFw7XXvPNOCHm3lcRZvtQJxtdCYEPbF24E=; b=Yqalx3KDiaDhGcGPB4tHEYNJ14AegH3Mwi95xJU443BmrMQc6hPI1Mev+JMcykLvDL 7PIxP4CYbnzQj1MeGj1AN5SLfw0SiFapK4GapV52P3lWKS+WHbA0+yysmMt6pjuTDfjQ JEAiqd160hMSoxkX3gSKp07eRnPwgMw/vEfJ94tfQjS7/6SKb+yC6JKpAqpIIHFAuoVE PWEikK/t12m8HO8QOPy5w36ZxnwWoZ8W80eNCkiY4kM+B5Izdlt7UhUAybukeG7yMS7h /3pvJl/3JVuG9nVjM9gcIkGSu25MaioZ68bz6yNwKXS1kWPtSY7H31oqxp5hroSTqyal tKJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762544187; x=1763148987; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6OoBSCQScNFw7XXvPNOCHm3lcRZvtQJxtdCYEPbF24E=; b=DFC8nMHAj3g5EkSLYT/PqP8i8B/cur/M1fZRLoMJODkIBRVfL/RF/Cfn6yU+h0Qwnn JJbUyQ4peP7Ersdynmh4AEBib+2knuUlKWKelugTnuWp59fJaezAQAcWtdZ4dsxhl3NQ E2xIme6Ei5o4UQQPqogJ/i0/ImmqWO/4NSgZfMAWdni3Pjy95+Ue1mtTVuOXWpVkF64U GCJuKAdghAotUF3ALNbJmurGVswdyUbVEkdVRz2teGOBxmE9CbXiYevKp8H/Y3y/X41V NcUXkxImHfwd2WuHoVil3FMNwA/UblSL0CXABcAgtDgwhUU7OogoSYDrbW4Tp37EF4qh 6z3Q== X-Forwarded-Encrypted: i=1; AJvYcCVbo4vtAdQA7/RBjnhFdJQeBdXeb3O1H3j7jtSkjK12Q0L2j6Cv6Rz+GO6EhvWUIk5i8HqibT3+Imw=@lists.php.net X-Gm-Message-State: AOJu0YyYhAZEEjG3SfW0E4IHKO2BKv6Zl4yeYV+YBpY2xLZZPwFt9hUy L2Ys2cvhou2PmJjpFpf3DgSzbmsqlxkebqXulVh8zvihSfxFVJMgJ7co X-Gm-Gg: ASbGncviiu4QRutiGDrstrKDOLPnsMG7xKdyyInA/TCLPmQGa1TrQAZm9bJ9QWq28oe xT0NKzpi8ENo3M+ZHD/UxxRux6ZCpf4/7pZLExSGRp8OgRVjtAbrS30jq5M3JIjswErJSwJK1ry Ukmzm8z7yD/YE/CkGJ+Hdb7GT+QxJvrUzSjGSdEMBpWT1SQB80b3aWq+Qba2aB+vKZBUwO3A/wq +P4b0U1hMU3JkzEKrjKMDCzl2islJT7RedxiznF4nCMCp051jbfdtrlPFQYOGgXkjbt40xQyiRN X0oRqooGQQ3BD5kV54cH3H2nIqNhiFnOYTxxPqjC7K0/2SpltR1M38FfLBxLd70ckLy3W550sB2 4gUJyDPJEy1Nq7XH319TJgjMxm7ZUgxNz8oxpSJT2OwBTzcucL5GXQ1+ysEeyajj0eubaz6Yfht qpNNg5cNl6TlXrYBde6g== X-Google-Smtp-Source: AGHT+IE5ihLdG+9Pv5qaSFmZSxzXfBDTKYoUlbvpG1sag/E45TViur93jdHd7dp1jQCICiezDdWfIA== X-Received: by 2002:a05:6000:2508:b0:425:7e45:a4df with SMTP id ffacd0b85a97d-42b2dbe2dfamr155933f8f.11.1762544186615; Fri, 07 Nov 2025 11:36:26 -0800 (PST) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42b2dd927d5sm77098f8f.24.2025.11.07.11.36.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Nov 2025 11:36:26 -0800 (PST) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_9A70A471-3ABA-4D2E-AD74-E91CA01EC5CF" Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: [PHP-DEV] [RFC][Discussion] use construct (Block Scoping) Date: Fri, 7 Nov 2025 20:36:15 +0100 In-Reply-To: Cc: Seifeddine Gmati , internals@lists.php.net To: Edmond Dantes References: X-Mailer: Apple Mail (2.3826.700.81) From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_9A70A471-3ABA-4D2E-AD74-E91CA01EC5CF Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 6 nov. 2025 =C3=A0 06:01, Edmond Dantes a = =C3=A9crit : >=20 > Hello! >=20 > ```php > function addStudentLessons(DatabaseTransaction $transaction) { > try { > $transaction->execute(...); > } catch(\Exception $e) { > Logger::log($e); > throw $e; > } > } >=20 > // Application Code: > function do_work(DatabasePool $pool): void { > using ( > $connection =3D $pool->getConnection(), > ) { > using ($transaction =3D $connection->beingTransaction()) { > $transaction->execute('...'); > sleep(10); // more work. > $transaction->execute('...'); > } >=20 > sleep(10); // more work > } >=20 > sleep(10); // more work > } // <=3D=3D broken! > ``` >=20 > Logger::log($e); <=3D=3D reason! >=20 > In this example, the `Logger` service holds the exception `$e`, > which completely breaks the code because the transaction will no > longer complete correctly, and it=E2=80=99s unclear when the resources = will be > released. > This is even more true for stateful applications, where the Logger > processes stored exceptions later rather than immediately. >=20 > Note that I didn=E2=80=99t even use circular references. I=E2=80=99m = sure that 90% of > PHP developers who see this code won=E2=80=99t even understand what = the > problem is. > And in practice, it will work for about 50% of them and fail for the = other 50%. >=20 > At the same time, as a programmer, I didn=E2=80=99t do anything = particularly > wrong or make any obvious mistake in this code. It=E2=80=99s just that = the > logging service holds the exception object for a while. >=20 > RC-managed objects were designed to create code where the destruction > time of an object cannot be determined statically (only at runtime). > Automatic memory management is not a primary feature of RC objects, > since it can be implemented without reference counting. >=20 > However, the code in the example pursues the opposite goals: it must > guarantee the exact moment a function is called. In other words, the > RAII concept is not suitable here. > And this situation is typical for PHP stateful applications, where > resource control is not managed through RAII. >=20 > --- > Best Regards, Ed Hi, Indeed, and here is a proof (variation on the =E2=80=9CExample showing = reliable resource management=E2=80=9D of the RFC): https://3v4l.org/KJaL1 Something like a `Disposable` interface as suggested in the Future scope = section, is probably the only way to make it reliable, and it ought to = be part of the RFC. =E2=80=94Claude --Apple-Mail=_9A70A471-3ABA-4D2E-AD74-E91CA01EC5CF Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Le 6 nov. 2025 =C3=A0 06:01, Edmond Dantes = <edmond.ht@gmail.com> a =C3=A9crit :

Hello!

```php
func= tion addStudentLessons(DatabaseTransaction $transaction) {
=     try {
=          $transaction->exe= cute(...);
    } catch(\Exception $e) {
=         Logger::log($e);
=         throw $e;
=     }
}

// Application Code:
function = do_work(DatabasePool $pool): void {
 using (
=    $connection =3D $pool->getConnection(),
 ) = {
   using ($transaction =3D = $connection->beingTransaction()) {
=      $transaction->execute('...');
=      sleep(10); // more work.
=      $transaction->execute('...');
=    }

   sleep(10); // more = work
 }

 sleep(10); // more work
} // <=3D=3D = broken!
```

Logger::log($e); <=3D=3D reason!

In this = example, the `Logger` service holds the exception `$e`,
which = completely breaks the code because the transaction will no
longer = complete correctly, and it=E2=80=99s unclear when the resources will = be
released.
This is even more true for stateful applications, = where the Logger
processes stored exceptions later rather than = immediately.

Note that I didn=E2=80=99t even use circular = references. I=E2=80=99m sure that 90% of
PHP developers who see this = code won=E2=80=99t even understand what the
problem is.
And in = practice, it will work for about 50% of them and fail for the other = 50%.

At the same time, as a programmer, I didn=E2=80=99t do = anything particularly
wrong or make any obvious mistake in this code. = It=E2=80=99s just that the
logging service holds the exception object = for a while.

RC-managed objects were designed to create code = where the destruction
time of an object cannot be determined = statically (only at runtime).
Automatic memory management is not a = primary feature of RC objects,
since it can be implemented without = reference counting.

However, the code in the example pursues the = opposite goals: it must
guarantee the exact moment a function is = called. In other words, the
RAII concept is not suitable here.
And = this situation is typical for PHP stateful applications, = where
resource control is not managed through = RAII.

---
Best Regards, = Ed


Hi,

Indeed, and here is a proof (variation on the = =E2=80=9CExample showing reliable resource management=E2=80=9D of the = RFC):


<= /div>
Something like a `Disposable` interface as suggested in the = Future scope section, is probably the only way to make it reliable, and = it ought to be part of the = RFC.

=E2=80=94Claude


= --Apple-Mail=_9A70A471-3ABA-4D2E-AD74-E91CA01EC5CF--