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
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.