/** * Register and run a shutdown handler. */ public function testRegister() { self::$testVariable = 0; $handler = new ShutdownHandler(array(get_class($this), 'shutdown'), array()); $this->assertTrue($handler instanceof ShutdownHandler); $handler->run(); $this->assertSame(1, self::$testVariable); }
public function testInvalidCallback() { try { $callback_name = null; do { $callback = 'invalidfunction' . md5(rand(0, 100000)); } while (ShutdownHandler::isCallable($callback, false, $callback_name)); $handler = new ShutdownHandler($callback, array()); $handler->run(); $this->assertTrue(false); } catch (\RuntimeException $e) { $this->assertEquals("Callback: '{$callback_name}' is not callable", $e->getMessage()); } }
<?php namespace Gielfeldt\ShutdownHandler\Example; require 'vendor/autoload.php'; use Gielfeldt\ShutdownHandler\ShutdownHandler; /** * Simple shutdown handler callback. * * @param string $message * Message to display during shutdown. */ function myshutdownhandler($message = '') { echo "Goodbye {$message}\n"; } // Register shutdown handler to be run during PHP shutdown phase. $handler = new ShutdownHandler('\\Gielfeldt\\ShutdownHandler\\Example\\myshutdownhandler', array('cruel world')); echo "Hello world\n"; // Register shutdown handler. $handler2 = new ShutdownHandler('\\Gielfeldt\\ShutdownHandler\\Example\\myshutdownhandler', array('for now')); // Don't wait for shutdown phase, just run now. $handler2->run();
/** * Run our shutdown handler upon object destruction. */ public function __destruct() { $this->shutdown->run(); }