Created
May 30, 2013 13:42
-
-
Save gcatlin/5677905 to your computer and use it in GitHub Desktop.
Simple PHP shutdown function manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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