简体   繁体   中英

Cannot use object of type Closure as array

fatal error " Cannot use object of type Closure as array", how to fix this problem. code i am using is as below

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');
      }
    });

the code is snippet from drupal message_broker module

Try use call_user_func_array

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]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM