189

回调是如何用 PHP 编写的?

4

9 回答 9

174

该手册可互换使用术语“回调”和“可调用”,但是,“回调”传统上是指充当函数指针的字符串或数组值,引用函数或类方法以供将来调用。自 PHP 4 以来,这允许了一些函数式编程元素。风格是:

$cb1 = 'someGlobalFunction';
$cb2 = ['ClassName', 'someStaticMethod'];
$cb3 = [$object, 'somePublicMethod'];

// this syntax is callable since PHP 5.2.3 but a string containing it
// cannot be called directly
$cb2 = 'ClassName::someStaticMethod';
$cb2(); // fatal error

// legacy syntax for PHP 4
$cb3 = array(&$object, 'somePublicMethod');

一般来说,这是使用可调用值的安全方法:

if (is_callable($cb2)) {
    // Autoloading will be invoked to load the class "ClassName" if it's not
    // yet defined, and PHP will check that the class has a method
    // "someStaticMethod". Note that is_callable() will NOT verify that the
    // method can safely be executed in static context.

    $returnValue = call_user_func($cb2, $arg1, $arg2);
}

现代 PHP 版本允许将上述前三种格式直接调用为$cb(). call_user_funccall_user_func_array支持以上所有。

请参阅: http: //php.net/manual/en/language.types.callable.php

注释/注意事项:

  1. 如果函数/类是命名空间的,则字符串必须包含完全限定的名称。例如['Vendor\Package\Foo', 'method']
  2. call_user_func不支持通过引用传递非对象,因此您可以使用call_user_func_array,或者在以后的 PHP 版本中,将回调保存到 var 并使用直接语法:$cb();
  3. 具有__invoke()方法(包括匿名函数)的对象属于“可调用”类别,并且可以以相同的方式使用,但我个人不会将这些与遗留的“回调”术语联系起来。
  4. legacycreate_function()创建一个全局函数并返回它的名字。它是一个包装器,eval()应该使用匿名函数。
于 2008-09-08T20:29:04.563 回答
82

使用 PHP 5.3,您现在可以这样做:

function doIt($callback) { $callback(); }

doIt(function() {
    // this will be done
});

最后是一个很好的方法。PHP 的一个很好的补充,因为回调很棒。

于 2010-03-26T14:05:30.630 回答
30

回调的实现是这样完成的

// This function uses a callback function. 
function doIt($callback) 
{ 
    $data = "this is my data";
    $callback($data); 
} 


// This is a sample callback function for doIt(). 
function myCallback($data) 
{ 
    print 'Data is: ' .  $data .  "\n"; 
} 


// Call doIt() and pass our sample callback function's name. 
doIt('myCallback');

显示:数据是:这是我的数据

于 2008-09-08T00:54:09.897 回答
9

我最近发现的一个绝妙技巧是使用 PHPcreate_function()创建一个匿名/lambda 函数以供一次性使用。它对于 PHP 函数(如 、 或使用回调进行自定义处理)array_map()很有preg_replace_callback()usort()。它看起来很像它eval()在幕后做的事情,但它仍然是使用 PHP 的一种很好的函数式方式。

于 2008-09-08T02:20:13.323 回答
7

好吧......随着 5.3 的到来,一切都会更好,因为在 5.3 中,我们将获得闭包和匿名函数

http://wiki.php.net/rfc/closures

于 2008-09-08T12:33:54.207 回答
7

您将需要验证您的呼叫是否有效。例如,对于特定函数,您需要检查该函数是否存在:

function doIt($callback) {
    if(function_exists($callback)) {
        $callback();
    } else {
        // some error handling
    }
}
于 2008-09-08T19:51:37.667 回答
5

create_function在课堂上对我不起作用。我不得不使用call_user_func.

<?php

class Dispatcher {
    //Added explicit callback declaration.
    var $callback;

    public function Dispatcher( $callback ){
         $this->callback = $callback;
    }

    public function asynchronous_method(){
       //do asynch stuff, like fwrite...then, fire callback.
       if ( isset( $this->callback ) ) {
            if (function_exists( $this->callback )) call_user_func( $this->callback, "File done!" );
        }
    }

}

然后,使用:

<?php 
include_once('Dispatcher.php');
$d = new Dispatcher( 'do_callback' );
$d->asynchronous_method();

function do_callback( $data ){
   print 'Data is: ' .  $data .  "\n";
}
?>

[编辑] 添加了一个缺少的括号。另外,添加了回调声明,我更喜欢这样。

于 2009-08-13T14:43:18.300 回答
3

每次create_function()在 php 中使用时,我都会感到畏缩。

参数是一个逗号分隔的字符串,整个函数体都在一个字符串中......啊......我认为即使他们尝试过,也无法让它变得更丑陋。

不幸的是,当创建命名函数不值得麻烦时,这是唯一的选择。

于 2008-09-08T12:26:49.357 回答
2

对于那些不关心破坏与 PHP 兼容性的人< 5.4,我建议使用类型提示来实现更简洁的实现。

function call_with_hello_and_append_world( callable $callback )
{
     // No need to check $closure because of the type hint
     return $callback( "hello" )."world";
}

function append_space( $string )
{
     return $string." ";
}

$output1 = call_with_hello_and_append_world( function( $string ) { return $string." "; } );
var_dump( $output1 ); // string(11) "hello world"

$output2 = call_with_hello_and_append_world( "append_space" );
var_dump( $output2 ); // string(11) "hello world"

$old_lambda = create_function( '$string', 'return $string." ";' );
$output3 = call_with_hello_and_append_world( $old_lambda );
var_dump( $output3 ); // string(11) "hello world"
于 2012-07-25T20:03:57.760 回答