TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-27-2008, 05:29 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Box 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());
}
Attached Files
File Type: php TalkPHP_Tripwire.php (1.0 KB, 73 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
Alan @ CIT (01-27-2008), sketchMedia (06-02-2008)
Old 01-27-2008, 05:41 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
Wildhoney (01-27-2008)
Old 01-27-2008, 05:54 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

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!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-27-2008, 06:08 PM   #4 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
xenon is on a distinguished road
Default

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
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-27-2008, 11:06 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Yeah Well aware of that. Thanks.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-28-2008, 12:03 AM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 824
Thanks: 163
Orc is on a distinguished road
Default

We need Zend core hackers. :P
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
Old 06-02-2008, 08:42 PM   #7 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

nifty and interesting bit of code m8, cheers for that.
__________________
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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

 
     

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