Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the anonymous function give me?

Tags:

php

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)
    );
}

?

like image 388
xiyom Avatar asked Mar 01 '18 19:03

xiyom


People also ask

What is an anonymous function used for?

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.

Why do we use anonymous function in Python?

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.

What do anonymous function do in JavaScript?

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.

Which function is known as anonymous function give example?

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();


2 Answers

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 ) { ... }
like image 43
Jaquarh Avatar answered Nov 05 '22 01:11

Jaquarh


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:

  1. Framework setup code is evaluated by $this->session->executeAtomically().
  2. Your code ($this->service->execute($request);) is evaluated by $this->session->executeAtomically().
  3. Framework teardown code is evaluated by $this->session->executeAtomically().

By evaluating the expression yourself rather than wrapping it in a closure, the code instead runs in the following order:

  1. Your code is evaluated by your method.
  2. Framework setup code is evaluated by $this->session->executeAtomically().
  3. Framework teardown code is evaluated by $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.

like image 89
Mikkel Avatar answered Nov 05 '22 01:11

Mikkel