aboutsummaryrefslogtreecommitdiff
path: root/include/defer.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/defer.php')
-rw-r--r--include/defer.php12
1 files changed, 12 insertions, 0 deletions
diff --git a/include/defer.php b/include/defer.php
new file mode 100644
index 0000000..ee01bbc
--- /dev/null
+++ b/include/defer.php
@@ -0,0 +1,12 @@
+<?php
+
+function defer(&$context, $f): void {
+ $context ??= new class() {
+ public $funcs = [];
+ public function __destruct()
+ {
+ while (count($this->funcs)) array_pop($this->funcs)();
+ }
+ };
+ $context->funcs[] = $f;
+}