On Mon, Nov 20, 2023 at 10:06 AM chopins xiao <chopins.x...@gmail.com> wrote:
>
> Similar to a tick event, an async function will be called automatically after 
> the block of code .
>  and the async function  is cleared when script shutown.
> Async function can be cleared after they have returned, or they can continue 
> to be called at the next event after they have returned.
> and some of the features of Fiber
>

I think you want a flattened

try {
  // do stuff
} finally {
  // clean up
}

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 = 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.

Robert Landers
Software Engineer
Utrecht NL

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to