note 60234 deleted from language.variables.predefined by nlopess

From: Date: Fri, 30 Dec 2005 19:50:44 +0000
Subject: note 60234 deleted from language.variables.predefined by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-101005@lists.php.net to get a copy of this message
Note Submitter: jon at fuck dot org ---- this is a handy suite of functions i wrote which manually register globals with callback functions for controlling user input. <?php /**register a key from a superglobal as a global variable in the current context. * @param $name string key to superglobal. * @param $type string typename to cast the value to. * @param $default mixed default value to set for new global. * @param $super &array reference to superglobal of choice. * could be any array with keys, though. * @param $callbacks array set of functions to modify the data * from the superglobal after registering. * @return boolean true if key found in superglobal, false otherwise. * note that the global is registered in either case. */ function register_global($name, $type = 'string', $default = null, &$super, $callbacks = null) { global ${$name}; // set default value ${$name} = $default; if (isset($super[$name])) { // get data from superglobal ${$name} = $super[$name]; // typecast settype(${$name}, $type); // normalize with callback functions if (is_array($callbacks)) foreach($callbacks as $func) ${$name} = call_user_func($func, ${$name}); // default overridden. return true; } // default value was set. return false; } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * example callback functions to be used with register_global(). */ // use only the first token of the input string; assume the // string might be urlencoded (a better way to do this is // obviously a regex). // function rg_tok($input) { return strtok($input, ' +'); } // enforce string is lowercase. // function rg_lcase($input) { return strtolower($input); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * example usage of register_global(): */ // set $foo to $_GET['foo'] or 42 if not specified. register_global('foo', 'integer', 42, $_GET); echo $foo; ?>

« previous php.notes (#101005) next »