TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Tripwire Class (Much like the Java "final" keyword for PHP) (http://www.talkphp.com/tips-tricks/2116-tripwire-class-much-like-java-final-keyword-php.html)

Wildhoney 01-27-2008 05:29 PM

Tripwire Class (Much like the Java "final" keyword for PHP)
 
1 Attachment(s)
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());
}

Alan @ CIT 01-27-2008 05:41 PM

You never know, could be useful in particular places :-)

More importantly, it does show a good use for the ticks/declare functions in PHP which are rarely used but very handy.

^^

Alan

Wildhoney 01-27-2008 05:54 PM

Karl said it's very much like the final keyword on variables that Java has - and PHP is missing, apart from on classes. So there we go - some use. And with that, I've used the construct global for the first time in a while!

xenon 01-27-2008 06:08 PM

PHP does actually have the final keyword implemented for classes and methods (not for properties). Just wanted to make sure you are aware of that :-P

Wildhoney 01-27-2008 11:06 PM

Yeah :-) Well aware of that. Thanks.

Orc 01-28-2008 12:03 AM

We need Zend core hackers. :P

sketchMedia 06-02-2008 07:42 PM

nifty and interesting bit of code m8, cheers for that.


All times are GMT. The time now is 08:07 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0