1

I'm making a transaction with Predis but I can't figure out how to pass my own variables into the anonymous transaction function.

$options = array();
$x = 13;
$transaction = $predis->multiExec($options, function($transaction) {
   //i need $x here
});

I've tried to add it to the options array but it's protected inside the anonymous function.

How can i do this?

Martin
  • 5,197
  • 11
  • 45
  • 60

1 Answers1

0

I found the solution. PHP supports importing variables to the closure with the use keyword.

So this is works:

$options = array();
$x = 13;
$transaction = $predis->multiExec($options, function($transaction) use($x) {
   //now i have a copy of $x here
});

More on this: http://php.net/manual/en/functions.anonymous.php

Martin
  • 5,197
  • 11
  • 45
  • 60