douzi2785 2013-04-07 06:42
浏览 126
已采纳

监视PHP变量以获得值的更改

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.

  • 写回答

2条回答 默认 最新

  • dongtong7990 2013-04-07 06:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败