Home  >  Article  >  Backend Development  >  What functions does PHP support callbacks?

What functions does PHP support callbacks?

coldplay.xixi
coldplay.xixiOriginal
2020-06-30 16:07:222710browse

PHP supports callback functions: 1. Anonymous function, code is [$server->on 'Request']; 2. Class static method, code is [static function test $req]; 3. Function, code is [my_onRequest $req].

What functions does PHP support callbacks?

PHP supports callback functions:

1, anonymous function

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

You can use use to pass parameters to anonymous functions

2, Class static method

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

3, Function

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

4. Object method

class A
{
    function test($req, $resp)
    {
        echo "hello world";
    }
}
$object = new A();
$server->on('Request', array($object, 'test'));

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What functions does PHP support callbacks?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn