function callme() {
//stuff
return function($type = '%') use (&$data) {
//stuff goes here
return $data;
};
}
如何传递参数以覆盖$type
我只需要一些例子。
function callme() {
//stuff
return function($type = '%') use (&$data) {
//stuff goes here
return $data;
};
}
如何传递参数以覆盖$type
我只需要一些例子。
当我阅读该问题时,我理解它,因为您想传递返回函数中的默认值。我虽然:
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"
首先,您调用callme()
以获取该功能。然后调用该函数并传递一个参数:
$fn = callme();
$fn("whatever you want to pass");
call_user_func(callme(), 'type argument here');