5

I am actually trying to monitor a PHP variable (may be as a separate thread but not possible in PHP) and fire a PHP function whenever the value of the variable changes.

eg: lets take a variable $_GLOBALS['foo']=1;

if at any point in the code, the value of $_GLOBALS['foo'] changes to something else, i want to fire a PHP function immediately.

The variable can be anywhere inside a loop or in a function,etc.

Why i want this: I have a variable which stores the last error occured as a text. If the value of the variable changes from "" to something else, i want to trigger an error. My LOGIC may seem a bit strange but this is what i would like to do.

Thanx in advance.

Edit: I tried: How to use monitors in PHP? and How can one use multi threading in PHP applications but does not seem to solve the problem.

The Code (Thought this could solve some of your doubts on my question):

public function addtag($tagarray,$uid,$tagtype="1")
{
    $dbobj=new dboperations();
    $uiobj=new uifriend();
    $tagid=$uiobj->randomstring(30,DB_SOCIAL,SOCIAL_TAG,tag_tagid);
    $c=0;
    foreach($tagarray as $tags)
    {
        $c++;
        $tagname=$tags["tagname"];
        $taguid=$tags["tagid"];
        $dbobj->dbinsert("INSERT INTO ".SOCIAL_TAG." (".tag_tagid.",".tag_fuid.",".tag_tuid.",".tag_tagname.",".tag_tagtype.") VALUES
                ('$tagid','$uid','$taguid','$tagname','$tagtype')",DB_SOCIAL);
    }
    if($c==0)
    {
        $lasterror="No tags were added";return "";
    }
    else
    {
        return $tagid;
    }
}

Here, if i call a error handling function instead of monitoring the variable, it wont be advisable in my case since the error handling function may do any operation like give alert and redirect to a page or any similar operation.

I asked this question cause, i thought what if the script does not reach the line return ""; It would affect the project's workflow. Thats what i am worried about.

And the variable i was talking about is $lasterror and i have many functions like this where $lasterror is used.

Community
  • 1
  • 1
Vignesh T.V.
  • 1,790
  • 3
  • 27
  • 48
  • 1
    A quick Google search brought this up, not sure if it's any good for you, but have a read through - http://www.alexatnet.com/articles/programming-events-php – francisco.preller Apr 07 '13 at 06:45
  • This seems similar http://stackoverflow.com/questions/385420/variable-watch-in-php but they did not get an answer. – Aiias Apr 07 '13 at 06:45
  • @francisco.preller Thanx for the quick reply. I went through it but in that case, we have to trigger the events manually, its not automatic. – Vignesh T.V. Apr 07 '13 at 07:01
  • I guess my biggest question is When exactly do you want to check this? Would it be unreasonable to check the $lasterror on page load? Or if the error is generated on page creation, check it as a last stop before sending the view. – David Houde Apr 07 '13 at 07:05
  • @vignesh what do you mean by 'automatic'? This isn't going to happen unless you program the behaviour in. What you are describing is a need for an event system. – vascowhite Apr 07 '13 at 07:07
  • @DavidHoude: i would like to check this whenever a change in variable value occurs, it can be at any time. – Vignesh T.V. Apr 07 '13 at 07:13
  • @vascowhite: yes and sorry for the phrase "automatic". what i would want to know is how to program this behavior in so that a function fires when the variable value changes. – Vignesh T.V. Apr 07 '13 at 07:15

2 Answers2

6

I saw this, so I built this:

https://github.com/leedavis81/vent

Should solve your problem.

Lee Davis
  • 4,685
  • 3
  • 28
  • 39
5

There is no built-in way to do this in PHP, and there's no easy way to add it. It doesn't really feel right for the way the language works anyway.

Instead of setting a variable, you could build a custom function that handles the error - or use PHP's built-in error handling functionality using a custom error handler.

Another error handling method which comes close to what you want to do (I think) is exceptions.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    Agreed - the best way would to use proper OOP and then log changes/accesses to the variable. – Nick Pickering Apr 07 '13 at 06:51
  • Thanx for the quick reply. I could do it but i think it might affect the project's workflow. If it does not, then this is not an issue. – Vignesh T.V. Apr 07 '13 at 07:02
  • @Pekka: I think you are right. I better change my workflow to use exceptions because there is no other way to do this in PHP since i researched a bit more now. Thanx a lot.. – Vignesh T.V. Apr 07 '13 at 07:06