01-27-2008, 05:29 PM
|
#1 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Tripwire Class (Much like the Java "final" keyword for PHP)
Well, I created this little class - not for any particular purpose, because let's be honest, it's pretty useless, but it is quite nifty! Basically, all you have to do is arm (or set) a variable, and then let the class do the rest. If the variable value changes from what it was when you set it, an exception will be thrown.
We use it like so:
php Code:
class Tripwire { private static $m_aVars; private static $m_bStarted; const MESSAGE = 'Variable "%s" has been modified from "%s" to "%s".'; public static function init () { declare (ticks = 1); self:: $m_bStarted = true; register_tick_function ( create_function ( null, 'Tripwire::check();' ) ); } public static function set ($szVar) { global $ {$szVar}; if(self:: $m_bStarted == false) { self:: init(); } self:: $m_aVars[] = array ( 'var' => $szVar, 'data' => $ {$szVar} ); } public static function check () { if(! isset(self:: $m_aVars)) { return; } foreach(self:: $m_aVars as $aVar) { global $ {$aVar[ 'var'] }; if($ {$aVar[ 'var'] } != $aVar[ 'data'] ) { throw new Exception (sprintf(self:: MESSAGE, $aVar[ 'var'], $aVar[ 'data'], $ {$aVar[ 'var'] })); } } }}try { $szText = 'Adam'; Tripwire:: set('szText'); $szText = 'Karl'; }catch (Exception $pEx){ die($pEx-> getMessage()); }
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|