Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130801 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 B31F81A00BC for ; Thu, 7 May 2026 22:01:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1778191265; bh=NuxvvUXm4k3jCGbDsRbD/WZzFXmhMNzEO4GYqLJ1J3A=; h=Date:Subject:To:References:From:In-Reply-To:From; b=SUoCKPmaMzls6yycuIWdAYxwLlVeEvji6KFKm9a/PU1hhzMENFROmHtvDodzynfdA jJzoxLN9szowNGh3TWVsCUeyi1YzsXINjKdIlAnvbxHWwgy40m5vlsmWHkwcHLcacZ ADCKd/Y0pYUZI95K/KMwm+UDm7aQ3FBNIvIqC2ttHyko+WUCVwdq2pgEMRJFSIgOKk j2c0OxvW1hekfzG4u2Q68cPcDKjk/sjBebZPey7r7v5UVwMonLGPf3vscmRLckTf1k qdd10PnPu4tNYYaVS75aLIwwGdMuEZvbKEmRQvLn5DHcpWunNcZQX1RDHiUNdtq77f POpw+Aez3EgfQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7A26618059F for ; Thu, 7 May 2026 22:01:04 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a5-smtp.messagingengine.com (fhigh-a5-smtp.messagingengine.com [103.168.172.156]) (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 ; Thu, 7 May 2026 22:01:04 +0000 (UTC) Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfhigh.phl.internal (Postfix) with ESMTP id 8C8461400169 for ; Thu, 7 May 2026 18:00:53 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Thu, 07 May 2026 18:00:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1778191253; x=1778277653; bh=req1mQkJdgmkImz4RUw7FUlCXLBpBkyjy8o9o8ubawY=; b= Z9U8rbpoV0FZIAjB8KkNkQfrJuFypAPhcc5cfxT+ZzDs5smgc1PcdnUoNpc+1cxY IVKMB07M41N8p57gN4GN69ee2zg0HvvrEnjVw0cM9voaW+Uj7vh4W0PPa/vJO7ZT SgQx9pJzPitUZWYtROk2rcThjHPGklKLLzolWrvdBFzdwElDz9KJ21RJdvQkznyU C5QNNHJDMRmuZUx+bx3ArDzStXQzIels5aT1b8FVMBojkXm922aSCv05SKgvRi2b fK2fqrQ0lZvuOWqcWVZYmoVqGoU7EuwH2bwiSzuYcucGO1sTGNpkTmWUAQSMCGdr m/hIpz5ZNHEA6spnrBP2cA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1778191253; x=1778277653; bh=r eq1mQkJdgmkImz4RUw7FUlCXLBpBkyjy8o9o8ubawY=; b=oyP8/+Yv/Nbb/8Ebu KuDCB5tE8fcfFWltUZo5PeA0ClTmJj40M6gInVrfTIqETFNIPXRkWDA9GkrJFeVN POp4TYPyFWi9QslXPucvYiFTH8vqYW5G3DJ5L0wB8zAJDWFLhACStfsGOm1YqEeq iyrcZNeZqXtgB0NsV3bo1OMQ7r0oPQ7MsH6j72WNKywq2WK4Fc6Qr8A4ynLrWmLR Crjot30+x+qgfLQmvBa+WHpUsPVhT0d8PhQr518KXFicR6L3J8FkqHqrSLbMchmN 5cYqQhh7BJ3cPyA+zLW9JJfCISXn3PwJ6+eCfV+a4PfyqFpntKev8aLpX98vKoSV qivng== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgddutdekieduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertd dtvdejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdfuceo ihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpeffke evudffuddvheejvdefkeelfedtudegfeehjeduheegieduffeggeegveefheenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphh hpsehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphho uhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 7 May 2026 18:00:52 -0400 (EDT) Message-ID: Date: Thu, 7 May 2026 23:00:51 +0100 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Scope functions To: internals@lists.php.net References: <0187D382-80FD-432C-B0D5-B0F9D8783419@hotmail.com> <6B83B07A-0344-4149-BFE9-7F3DD21BBA39@rwec.co.uk> <82D0F837-81DA-48D5-B467-35A8CCF52F9B@hotmail.com> Content-Language: en-GB In-Reply-To: <82D0F837-81DA-48D5-B467-35A8CCF52F9B@hotmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 07/05/2026 13:16, Bob Weinand wrote: > I am not opposed at all to such a consistent macro system though. > ... but I would not make a transaction like that a macro. For inlining variables in a html or sql snippet with proper escaping, definitely great. > To me, macros should meaningfully transform their contained code rather than wrapping a bit of logic around, which would be covered by this RFC. > It's complimentary, not a replacement. Thinking about this further, I agree that full-blooded macros would be overkill for this case, but my starting point was that closures don't feel like the right solution either - they're a different feature being bent into shape. So rather than starting with a Closure, and taking things away, I was trying to think of ways to start with a simple block of code, and build it up. For instance, the array_walk example in the RFC is just an ugly (and probably inefficient) way of writing a foreach loop: array_walk($numbers, fn($number) {     $count = bcadd($count, '1');     $sum = bcadd($sum, $number); }); foreach($numbers as $number) {     $count = bcadd($count, '1');     $sum = bcadd($sum, $number); } So, what would it look like to generalise that, so we could do the same for other scenarios, like async() and transaction()? A simple first step would be something that could take a block of code, and say when to execute it: custom_block transaction($dbConnection) {     try {         $dbConnection->beginTransaction();         __execute_block_body();     }     catch (\Throwable $e) {         $dbConnection->rollbackTransaction();         throw $e;      }      $dbConnection->commitTransaction(); } transaction($myDb) {     $myDb->query('UPDATE ...'); } If the block body is never represented as a variable, there's no need to define what happens when it outlives scope, or the user tries to clone it, rebind it, etc. And if the calling code doesn't look like it's creating a Closure, users won't have any wrong expectations about variable scopes or lifetimes. Onto that, we can add syntax to push values into the block, e.g. using the "as" keyword like in "foreach" (example based on the "Generator decorator managers" section at the end of the Context Managers RFC): custom_block opening($filename) {     $f = fopen($filename, "r");     if (!$f) {         throw new Exception("fopen($filename) failed");     }     try {         __execute_block_body($f);     } finally {         fclose($f);     } } opening(__FILE__ as $f) {     var_dump($f); } And some way to pull values out, e.g. a "break with" keyword: custom_block transaction(DBConnection $conn) {   $conn->beginTran();   try {     if (__execute_block_body() === DBConnection::TRANSACTION_ABORT) {       $conn->rollbackTran();       return;     }   } catch (\Throwable $e) {     $conn->rollback();     throw $e;   }   $conn->commitTran(); } transaction($connection) {     $affectedRows = $connection->query("UPDATE ...");     if ($affectedRows === 0) {         break with DBConnection::TRANSACTION_ABORT;     }     // ... } I'm sure there are details here that wouldn't quite work as expressed, but hopefully it explains what I meant by wanting something closer to macros than closures. Regards, -- Rowan Tommins [IMSoP]