以Swoole服务事件回调为例

匿名函数

1
2
3
$server->on('Request', function ($req, $resp) {
echo "hello world";
});

类静态方法

1
2
3
4
5
6
7
class A {
static function test($req, $resp){
echo "hello world";
}
}
$server->on('Request', 'A::Test');
$server->on('Request', array('A', 'Test'));

函数

1
2
3
4
function my_onRequest($req, $resp){
echo "hello world";
}
$server->on('Request', 'my_onRequest');

对象方法

1
2
3
4
5
6
7
8
class A {
function test($req, $resp){
echo "hello world";
}
}

$object = new A();
$server->on('Request', array($object, 'test'));