I have a closure as follows:
public function create(){
DB::transaction(function(){
return 'function called'
});
how can I return "function called" from create
?
I have a closure as follows:
public function create(){
DB::transaction(function(){
return 'function called'
});
how can I return "function called" from create
?
You could assign this value to a main method's variable:
this is depends on the implementation of the closure ,
take this quick example:
so, how this could be useful ?
-- some time you want give the client user of your object the power to use some of your object methods or properties without any effect on your object context, you need your method to return something else , in the same time that you need to give the user access to do something within your object/method, it's a kind of dependency injection so to speak .
live example for this kind of implementing, the difference between array_map and array_walk internal functions in
php
array_walk :
array_map :
it's kind of : the following is just a pseudo-code
for example:
this will output:
for your example : to get the value from inside the callback you may pass a variable to your callback by reference then edit it's value from inside the closure then use this variable outside the closure as you want as follows:
Conclusion :-
this is depends on the implementation of the function/method.