Re: Static Destructor methods

From: Date: Sat, 06 Apr 2002 03:25:03 +0000
Subject: Re: Static Destructor methods
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-5208@lists.php.net to get a copy of this message
Richard Heyes wrote: > > In the File class, which is soon to be entirely static based, there is a > need for a destructor style method which runs when the script is finished to > unlock locked files, and close open file pointers. Currently it's done by > using a File_shutDown() function which is registered, which in turn calls > File::_File(). This is definetly a need in my point of view. Now static classes can't take advantage of PEAR destructors. For example I would like to use it to remove automatically a temporary directory created by System::mkTmp(). > What I'd like to suggest is a function in PEAR.php which can do this. For > example, at the bottom of File.php, outside the class, there'd be a call to: > > PEAR::registerStaticShutdown($className, $methodName); I'd preffer: PEAR::registerShutdownFunc($func, $args); which accepts the same params of call_user_func_array(). > This function could in turn store the class/method combinations and call > them when required. It would require a call from _PEAR_call_destructors() > adding (eg. PEAR::registerStaticShutdown(null, null, true) ), and a possible > prototype for the function could be (as a method of class PEAR): > > function registerStaticShutdown($class, $method, $shutdown = false) > { > static $staticDestructors; > > if (!$shutdown) { > $staticDestructors[] = array('class' => $class, 'method' => > $method); > } else { > foreach ($staticDestructors as $val) { > eval(sprintf('%s::%s;', $class, $method)); > } > } > } > Why not just do as the current method for object does? Ej: function registerShutdowFunc($func, $args) { $GLOBALS['_PEAR_Static_Funcs'][] = array($func, $args); } function _PEAR_call_destructors() { ... foreach($GLOBALS['_PEAR_Static_Funcs'] as $func => $args) { call_user_func_array($func, $args); } } So I could do inside my class: function mkTemp($dir) { PEAR::registerShutdownFunc(array('System', 'rm'), "-rf $dir"); } That will be also useful for userland code because the actual register_shutdown_function() can't register objects, static methods or arguments. Tomas V.V.Cox

« previous php.pear.dev (#5208) next »