致命错误“无法使用Closure类型的对象作为数组”,如何解决此问题。我正在使用的代码如下
function message_broker_example_message_broker_consumers($self_name) {
$consumers = array();
// An example consumer implemented using a closure.
$consumers['helloWorldToEveryone'] = array(
'queue' => 'helloWorldForAll',
'callback' => function($message, \Closure $ack) {
$message = json_decode($message->body);
if ($message->name == 'crash') {
throw new InvalidMessageException('Invalid name detected!');
}
if (function_exists('drush_print')) {
drush_print('Hello world, ' . $message->name);
}
else {
drupal_set_message(t('Hello world, @name.', array('@name' => $message->name)));
}
$ack();
}, 'invalidMessageHandler' => function($message) {
if (function_exists('drush_print')) {
drush_print('Invalid message handler was executed.');
}
else {
drupal_set_message(t('Invalid message handler was executed.'), 'warning');
}
});
代码是来自drupal message_broker模块的片段
答案 0 :(得分:1)
function message_broker_example_message_broker_consumers($self_name) {
$consumers = array();
// An example consumer implemented using a closure.
$consumers['helloWorldToEveryone'] = array(
'queue' => 'helloWorldForAll',
'callback' => call_user_func_array(function($message, \Closure $ack) {
$message = json_decode($message->body);
if ($message->name == 'crash') {
throw new InvalidMessageException('Invalid name detected!');
}
if (function_exists('drush_print')) {
drush_print('Hello world, ' . $message->name);
}
else {
drupal_set_message(t('Hello world, @name.', array('@name' => $message->name)));
}
$ack();
}, [$message, $ack]),
'invalidMessageHandler' => call_user_func_array(function($message) {
if (function_exists('drush_print')) {
drush_print('Invalid message handler was executed.');
}
else {
drupal_set_message(t('Invalid message handler was executed.'), 'warning');
}
}, [$message]));