Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121800 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 52111 invoked from network); 24 Nov 2023 10:27:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 10:27:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8714718002F for ; Fri, 24 Nov 2023 02:27:14 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,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.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (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, 24 Nov 2023 02:27:11 -0800 (PST) Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-dae7cc31151so1513714276.3 for ; Fri, 24 Nov 2023 02:27:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700821625; x=1701426425; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=HOiHUDSWaF1g+xikwcAH+uazfJ9j3dVAT+9fPbAgJwo=; b=Kd2dUFSEf+/aPaCp3Z61nUss7MvIaQToEYPr+7SXI2F0du5dpc9fmVJFdcrGeC5NJ9 Q1kTYABsGcRTde2/vBZzuYLQbGvItwy6yzSyzzNeyuS6JSPAdo3Rh35Yecg6aoWeoQo1 EX191yfATWmABnW2uiUg6gtu7HmBDAcFEddhGq2HygU4t6rIfIwVnM8owI5wGm3I3rvB 6HlthApYoxlL/r2q4jOL+S+V1Z71eJ2wGlkIcRVGs/c91hE9IYmJ3yY9wQT6rAek/T75 qaxQ4OYF/543j+6xkIhMvnu23oUHbI4GQ8iC3ANMHOGklUCVunoV5pNjfnSs2GKHF4js +EQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700821625; x=1701426425; 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=HOiHUDSWaF1g+xikwcAH+uazfJ9j3dVAT+9fPbAgJwo=; b=C+rxcx8U7yN2e9VsHf5Gy8R5FMgHj0U4Lx8wWrncpq7kagIrcXH+o7Hd09VVQAwcN/ ob12PjRUhjIm9HEbcg6bZapvdxmsZksS2Ab3J22fYPf/89jktyXi8GDSJbJu/5t4zr00 5x8/0osZg9B3mfswY7RpocM4VkiQUz0bJHN2BwEk5wOUWmQdHuiUsql1GotBbNNUC0fg Y+m7h0/l6p3d/Xo3nHX9dxzlqYpAafY2P/IOCfKWq1w8/dQPSOsmtk2fcIWLzy14O2wh G/watbYJXAE/GLGQrTo6r+81VtWgRLq4aznUaqf6d3b4EGEmeUvWEQKrJp97FCT1FPPw PKCQ== X-Gm-Message-State: AOJu0Yyig/GVO0fJVBj8buw0m0npBKyogxIWTuMe+R4mxs42EizxjL8L LQjl8os6oF+dSuKLfHHXSTJUNBm/IFnlZZb34fQ= X-Google-Smtp-Source: AGHT+IHbvsKryITrJsTeiWtSa+XpD8uGXyr9HFlR/lw+nSgU3oZLt/6kK44U3/IbA1UvXYXdqWEwKHVX3X7w8oQtN8s= X-Received: by 2002:a25:81d0:0:b0:d9a:51d7:2d1c with SMTP id n16-20020a2581d0000000b00d9a51d72d1cmr1678213ybm.45.1700821625409; Fri, 24 Nov 2023 02:27:05 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Nov 2023 11:26:54 +0100 Message-ID: To: Robert Landers Cc: chopins xiao , "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000cf159d060ae36422" Subject: Re: [PHP-DEV] Is it possible to add asynchronous loop call function ? From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=) --000000000000cf159d060ae36422 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Robert, pt., 24 lis 2023 o 10:24 Robert Landers napisa=C5=82(a): > ... > You can also emulate this with: > > class Defer { > private function __construct(private \Closure $callback) {} > public function __destruct() { $this->callback(); } > public static function _(\Closure $callback) { return new > self($callback); } > } > > and use it like: > > function writeSomeStuff() { > // open files > $deferred =3D Defer::_($closeFiles(...)); > // do stuff > } > > So long as a reference exists to $deferred variable, the deferred > method won't be run. If the variable is local to the method/function > being run it, the deconstructor will be called after the function > returns. > > It isn't the most beautiful thing in the world, and easy to forget to > store the result of Defer, but it is handy sometimes. > This is interesting which makes me thinking if forget to store it could be prevented. I think requiring a ref could help with that: class Defer { private function __construct(private \Closure $callback) {} public function __destruct() { ($this->callback)(); } public static function _(\Closure $callback, &$var) { $var =3D new self($callback); } } $deferred =3D Defer::_($closeFiles(...), $foo); Without $foo there'd be an ArgumentCountError. Cheers, Micha=C5=82 Marcin Brzuchalski --000000000000cf159d060ae36422--