0

Somewhere in my __call magic method I'm invoking transactional, accepting a Closure:

try { $that = $this $this->conn->transactional(function ($conn) use ($that, $realMethod) { $result = call_user_func([$that, $realMethod], $conn); $this->conn->exec('SET foreign_key_checks = 1'); }); } catch (\Exception $e) { $this->conn->exec('SET foreign_key_checks = 1'); throw $e; } 

Is there any way to return $result from inside the Closure (or using pass by reference, etc.)?

Method $conn->transactional is not under my control:

public function transactional(Closure $func) { $this->beginTransaction(); try { $func($this); $this->commit(); } catch (Exception $e) { $this->rollback(); throw $e; } } 

1 Answer 1

2

Sure there is -- create a local variable inside the try block and capture it by reference:

$result = null; $that = $this; $this->conn->transactional(function ($conn) use ($that, $realMethod, &$result) { $result = call_user_func([$that, $realMethod], $conn); $this->conn->exec('SET foreign_key_checks = 1'); }); return $result; 

Of course this assumes that transactional will call its argument on the spot, since that's the case indeed we can all be happy.

Sign up to request clarification or add additional context in comments.

1 Comment

Figured it out right after posting the question. For some strange reason PHPStorm gave me an error, letting me think it wasn't possible. Thanks!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.