Four types of callback functions in OpenSwoole server

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Four types of callback functions

Closure
<?php
$server->on('request', function($request, $response){
    echo "hello world\n";
});
Static method
<?php
class A
{
    static function test($request, $response)
    {
        echo "hello world\n";
    }
}
$server->on('request', 'A::test');
$server->on('request', array('A', 'test'));
Function
<?php
function my_onResquest($request, $response)
{
    echo "hello world\n";
}
$server->on('request', "my_onResquest");
Class method
<?php
class A
{
    function test($request, $response)
    {
        echo "hello world\n";
    }
}
$object = new A();
$server->on('request', array($object, 'test'));
Last updated on September 21, 2022