doujubeng2942 2017-03-09 16:40
浏览 129

全局变量/对象上的pcntl_signal(php)

I'm trying to catch a signal (ie SIGUSR1) and call a function from an object.

class obj {
...
  public function debug() {
    var_dump($this->varfoo);
  }
}

$o = new obj();
declare(ticks = 1);
function sig_handler($signo) {
    switch ($signo) {
        case SIGUSR1:
            echo "Caught SIGUSR1...
";
            $o->debug();
            break;
        default:
            // handle all other signals
    }
}
pcntl_signal(SIGUSR1, "sig_handler");

As soon as I send the signal, I get a php fatal error: Call to a member function debug();

So, I tried something else:

Instead of that:

function sig_handler($signo) {
...
}

pcntl_signal(SIGUSR1, "sig_handler");

I used that:

pcntl_signal(SIGUSR1, function ($signal, $o) {
    echo gettype($o); //this prints null, I was hoping for object
    echo "Caught SIGUSR1...
";
    $o->debug();
});

I know it doesn't look right, but I can't figure out how to pass the $o inside the signal handler.

Thank you

  • 写回答

1条回答 默认 最新

  • duan00529 2017-03-10 09:01
    关注

    It worked by adding the handler inside the class like this:

    class obj {
    ...
    public function __construct() {
        ...
        pcntl_signal(SIGUSR1, function ($signal) { 
            echo "Caught SIGUSR1...
    ";
            $this->debug();
        });
    }
    

    Not sure if it's the best way, but seems to be working.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染