I have in tutorial:
public function execute(BaseRequest $request)
{
$operation = function() use($request) {
return $this->service->execute($request);
};
return $this->session->executeAtomically(
$operation->bindTo($this)
);
}
What does the anonymous function give me?
Why can't I just do:
public function execute(BaseRequest $request)
{
$operation = $this->service->execute($request);
return $this->session->executeAtomically(
$operation->bindTo($this)
);
}
?
An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.
These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions. Lambda forms can take any number of arguments but return just one value in the form of an expression.
In JavaScript, an anonymous function is that type of function that has no name or we can say which is without any name. When we create an anonymous function, it is declared without any identifier. It is the difference between a normal function and an anonymous function.
An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();
In this context, it defeats the purpose of anonymous functions however, they're used to extend the functions scope. Since the instance of the $request
variable isn't being passed through the function, it would be inaccessible without the use()
. Only anonymous functions allow the use()
clause to be used, therefore in another context it would be useful if you have global variables which you don't want to continuously pass through a function.
Example:
$someDatabase = new PDO();
$doSomeQuery = function( $sql, $bind = [] ) use ( $someDatabase )
{
$stmt = $someDatabase->Prepare( $sql );
return $stmt->execute( $bind );
};
// Now we never have to pass the connection in which the developer may not need to know
foreach( $doSomeQuery( 'SELECT * FROM tbl WHERE col = ?', ['value'] )->fetchAll() as $row ) { ... }
I don't know what framework you're working with here, but my assumption is that executeAtomically
has some setup and teardown logic included in it, probably starting and committing a database transaction. In such a case, you want the ultimate order of execution of your method to be:
$this->session->executeAtomically()
.$this->service->execute($request);
) is evaluated by $this->session->executeAtomically()
.$this->session->executeAtomically()
.By evaluating the expression yourself rather than wrapping it in a closure, the code instead runs in the following order:
$this->session->executeAtomically()
.$this->session->executeAtomically()
.Most real-world use cases of closures involve some sort of delayed execution, either to perform setup/teardown logic, conduct "lazy loading", execute code multiple times as in a loop, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With