Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Closure::bind() in PHP

Tags:

closures

php

The PHP manual offers little explanation about Closure::bind() and the example was confusing too.

Here's the code example on the site:

class A {
private static $sfoo = 1;
private $ifoo = 2;
}
$cl1 = static function() {
return A::$sfoo;
};
$cl2 = function() {
return $this->ifoo;
};

$bcl1 = Closure::bind($cl1, null, 'A');
$bcl2 = Closure::bind($cl2, new A(), 'A');
echo $bcl1(), "\n";
echo $bcl2(), "\n";

What are the parameters for Closure::bind()?

Null was used in the above, and even the "new" keyword was used too, which makes this even more confusing to me.

like image 926
Michael Akinlaby Avatar asked Sep 15 '17 14:09

Michael Akinlaby


People also ask

What is bind function in PHP?

The PDOStatement::bindValue() function is an inbuilt function in PHP that is used to bind a value to a parameter. This function binds a value to the corresponding named or question mark placeholder in the SQL which is used to prepare the statement.

What is closure function PHP?

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

What is object closure PHP?

Basically a closure in PHP is a function that can be created without a specified name - an anonymous function. Here's a closure function created as the second parameter of array_walk() . By specifying the $v parameter as a reference one can modify each value in the original array through the closure function. <?

What is PHP closure class?

The Closure class ¶Class used to represent anonymous functions. Anonymous functions yield objects of this type. This class has methods that allow further control of the anonymous function after it has been created. Besides the methods listed here, this class also has an __invoke method.


1 Answers

If you put the value of $cl2 as a method of class A, the class looks like this:

class A {
    public $ifoo = 2;

    function cl2()
    {
        return $this->ifoo;
    }
}

and you can use it like this:

$x = new A();
$x->cl2();
# it prints
2

But, because $cl2 is a closure and not a member of class A, the usage code above does not work.

The method Closure::bindTo() allows using the closure as it were a method of class A:

$cl2 = function() {
    return $this->ifoo;
};

$x = new A();
$cl3 = $cl2->bindTo($x);
echo $cl3();
# it prints 2

$x->ifoo = 4;
echo $cl3();
# it prints 4 now

The closure uses the value of $this but $this is not defined in $cl2. When $cl2() runs, $this is NULL and it triggers an error ("PHP Fatal error: Using $this when not in object context").

Closure::bindTo() creates a new closure but it "binds" the value of $this inside this new closure to the object it receives as its first argument.

Inside the code stored in $cl3, $this has the same value as the global variable $x. When $cl3() runs, $this->ifoo is the value of ifoo in object $x.

Closure::bind() is the static version of Closure::bindTo(). It has the same behaviour as Closure::bindTo() but requires an additional argument: the first argument must be the closure to bind.

like image 196
axiac Avatar answered Oct 06 '22 00:10

axiac