-1
function callme() {
    //stuff
    return function($type = '%') use (&$data) {
        //stuff goes here
        return $data;
    };
}

如何传递参数以覆盖$type

我只需要一些例子。

4

3 回答 3

1

当我阅读该问题时,我理解它,因为您想传递返回函数中的默认值。我虽然:

function callme($default_type = '%') {
    //stuff
    return function($type = $default_type) use (&$data) {
        print "$type\n";
        //stuff goes here
        return $data;
    };
}

但这是一个语法错误。那么最好的方法是做这样的事情:

function callme($default_type = '%') {
    //stuff
    return function($type = null) use (&$data, $default_type) {
        if( $type === null )
                $type = $default_type;

        print "$type\n";
        //stuff goes here
        return $data;
    };
}

$fn = callme("maybe");
$fn();                   // prints "maybe"
$fn("Carly Rae Jepsen"); // prints "Carly Rae Jepsen"
于 2013-09-19T22:05:07.007 回答
1

首先,您调用callme()以获取该功能。然后调用该函数并传递一个参数:

$fn = callme();
$fn("whatever you want to pass");
于 2013-09-19T21:28:31.473 回答
0
call_user_func(callme(), 'type argument here');
于 2013-09-19T21:28:43.287 回答