Skip to content

Instantly share code, notes, and snippets.

@gcatlin
Created May 30, 2013 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcatlin/5677905 to your computer and use it in GitHub Desktop.
Save gcatlin/5677905 to your computer and use it in GitHub Desktop.
Simple PHP shutdown function manager
<?php
class ShutdownFunctionManager {
protected $callbacks = array();
protected $registered = false;
public function __construct($auto_register = true) {
if ($auto_register) {
$this->register();
}
}
public function append($callback, $parameter = null) {
$this->callbacks[] = $this->parseArgs(func_get_args());
}
public function clear() {
$this->callbacks = array();
}
public function parseArgs($args) {
return array($args[0], array_slice($args, 1));
}
public function prepend($callback, $parameter = null) {
array_unshift($this->callbacks, $this->parseArgs(func_get_args()));
}
public function register() {
if (!$this->registered) {
register_shutdown_function(array($this, 'shutdown'));
$this->registered = true;
}
}
public function shutdown() {
foreach ($this->callbacks as $callback) {
call_user_func_array($callback[0], $callback[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment