2

I need to process a parameter in a batch function where the result will be kept after the function ends and it should be then passed in the finished function of the same batch. Is there a way to pass the parameters by a reference, so that any changes to that parameter are saved?

Example:

$params = [$a, $b];
$finishedParams = [$a, $b];
$batch = ['title' => 'Sample processing batch',
          'operations' => ['someFunction', $params],
          'finished' => ['finishedFunction', $finishedParams]
         ];
...
public static function someFunction(&$a, $b){
    $a = 'somethingChanged';
}
public static function finishedFunction($success, $results, $operations){
   // Use $a variable when somethingChanged after the batch process.
}
  • 1
    The way to do this is to put the result in the results entry of the context, not to attempt passing by reference. – Alfred Armstrong Jan 13 at 9:44
2

Alfred is right. You can achieve that by using the $context parameter.

public static function someFunction($a, $b, &$context) {
  // Store data in $context['results'] entry.
  // my_var will keep that value.
  $context['results']['my_var'] = 20;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.