Skip to content

Commit

Permalink
Explicitly define the order of the execution of deferred callbacks (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
djunny committed Jul 18, 2022
1 parent 48d3718 commit 1aadc6a
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@

function defer(?SplStack &$context, callable $callback): void
{
$context = $context ?? new SplStack();

$context->push(
new class($callback) {
private $callback;

public function __construct(callable $callback)
{
$this->callback = $callback;
}

public function __destruct()
{
\call_user_func($this->callback);
$context ??= new class() extends SplStack {
public function __destruct()
{
while ($this->count() > 0) {
\call_user_func($this->pop());
}
}
);
};

$context->push($callback);
}

0 comments on commit 1aadc6a

Please sign in to comment.