In the previous article, we studied the use of anonymous functions. For those who have not seen anonymous functions, you can enter the portal to learn about the use of closures. You are OUT of date.

A typical problem with closure anonymous functions in JS is to bind them to the this scope. This problem also exists in PHP, such as the following code:

$func = function($say){
    echo $this->name, ':'.$say, PHP_EOL;
};
$func('good'); // Fatal error: Uncaught Error: Using $this when not in object context 
Copy the code

In this anonymous function, we use \ this−>name to get the name attribute in the current scope, but who is this? We didn’t define it, so it’s going to be an error. The error message is: Who used this? We didn’t define it, so it’s going to be an error. The error message is: Who used this? We didn’t define it, so it’s going to be an error. The error message is: this is used but there is no object context, meaning no scope is specified for the $this reference.

The bindTo() method binds $this

Ok, so we give it a scope, just like JS, using a bindTo() method.

$func1 = $func->bindTo($lily.'Lily');
// $func1 = $func->bindTo($lily, Lily::class);
// $func1 = $func->bindTo($lily, $lily);
$func1('cool');
Copy the code

This will output normally. The bindTo() method copies the current closure object and binds it to the this and class scopes. Where, the this scope and the class scope. Where, the this scope and the class scope. Where, the lily argument is an object newThis argument, which is to specify newthis for the anonymous function that was copied. The second argument, ‘Lily’, binds to a new class scope, which represents a type and determines which private and protected methods can be called in the anonymous function. All three methods can be used to define this parameter. If we do not give this parameter, then we cannot access the private $name attribute:

$func2 = $func->bindTo($lily);
$func2('cool2'); // Fatal error: Uncaught Error: Cannot access private property Lily::$name
Copy the code

The call() method binds $this

Since PHP7, PHP has added the call() method to bind anonymous functions to $this. Let’s take a look at how it differs from the bindTo() method.

$func->call($lily.'well'); / / Lily: well
Copy the code

The forehead…

Doesn’t feel so much easier. First, it executes directly without assigning to a variable, that is, it does not copy the closure but executes directly; Second, there is no concept of class scope. The first argument still specifies the new $this reference, and the following arguments are the arguments of the original closure function.

While convenient, it also presents another problem, because there is no class scope limitation, so encapsulation can be broken. Your hard-won object-oriented design encapsulates a bunch of properties and then exposes all the private and protected contents of the object using this call(). Of course, it depends on our own business situation, after all, we are free to choose between the two when writing code.

conclusion

All of these features, including closure functions, are very JS like. This is also a trend of language integration. Whether we learn JS to see these features of PHP or learn PHP first and then see JS, it will make it easier for us to understand their role and ability, which is the benefit of language feature integration. Anyway, just learn and keep on coming!!

Test code: github.com/zhangyue050…

Reference documents: www.php.net/manual/zh/f… www.php.net/manual/zh/c… www.php.net/manual/en/c…