1. 程式人生 > >4種PHP回調函數風格

4種PHP回調函數風格

server object pre ray pan 回調 ring 對象方法 ble

4種PHP回調函數風格

匿名函數

$server->on(‘Request‘, function ($req, $resp) use ($a, $b, $c) {
    echo "hello world";
});

可使用use向匿名函數傳遞參數

類靜態方法

class A
{
    static function test($req, $resp)
    {
        echo "hello world";
    }
}
$server->on(‘Request‘, ‘A::Test‘);
$server->on(‘Request‘, array(‘A‘, ‘Test‘));

函數

function my_onRequest($req, $resp)
{
    echo "hello world";
}
$server->on(‘Request‘, ‘my_onRequest‘);

對象方法

class A
{
    function test($req, $resp)
    {
        echo "hello world";
    }
}

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

4種PHP回調函數風格