How to understand closure bind and bindto in PHP

Source: Internet
Author: User

Bind is a static version of BindTo, so just say bind. (not too much to know why to make two versions)

Official document: Copy a closure that binds the specified $this object and class scope.

In fact, the latter half of the sentence is not clear. My understanding: The method of converting a closure into a class (this method does not need to be called by the object), so that the $this, static, self in the closure are converted to the corresponding object or class.

Because there are several situations:

1. Bind only $this objects.
2. Bind the class scope only.
3. Bind $this objects and class scopes at the same time. (the document says)
4, are not bound. (Such a dozen is purely copy, the documentation is used cloning instead of bind or bindto)

Here's a detailed explanation of the following:

1. Bind only $this objects

$closure = function ($name, $age) {    $this->name = $name;    $this->age = $age;}; Class Person {public    $name;    public $age;    Public function Say () {        echo ' My name is {$this->name}, I ' m {$this->age} years old.\n ";    }} $person = new Person (),//Bind $this in $closure to $person//so that setting name and age in $bound_closure is actually setting $person name and age// That is, the specified $this object ($person) is bound $bound _closure = Closure::bind ($closure, $person); $bound _closure (' php '); $person Say ();

  

My name is PHP, I ' m years-old.

Note: In this example above, it is not possible to use static in $closure, if you need to use static, pass the third argument to the class name with the namespace.

2. Bind the class scope only.

$closure = function ($name, $age) {  static:: $name =  $name;  Static:: $age = $age;}; Class Person {    static $name;    static $age;    public static function say ()    {        echo ' My name is '. Static:: $name. ", I ' m". Static:: $age. "Years old.\n";    }} Bind the static in the $closure to the person class//So when you set name and age in $bound_closure, you actually set the name and age//of the person to bind the specified static (person) $ Bound_closure = Closure::bind ($closure, NULL, Person::class), $bound _closure (' php ', 100); Person::say ();

  

My name is PHP, I ' m years-old.

Note: In the above example, it is not possible to use $this in $closure because our bind binds only the class name, which is static, if you need to use $this, a new object is passed in as the second parameter of BIND.

3. Bind $this objects and class scopes at the same time. (the document says)

$closure = function ($name, $age, $sex) {    $this->name = $name;    $this->age = $age;    Static:: $sex = $sex;}; Class Person {public    $name;    public $age;    static $sex;    Public function say ()    {        echo "My name is {$this->name}, I ' m {$this->age} years old.\n";        echo "Sex:". Static:: $sex. ". \ n";    }} $person = new Person (),//The static in $closure is bound to the person class, $this bound to $person object $bound_closure = Closure::bind ($closure, $ person, person::class); $bound _closure (' php ', ' female '); $person->say ();

  

My name is PHP, I ' m years-old. Sex:female.

In this example, you can use both $this and static in $closure

4, are not bound. (Such a dozen is purely copy, the documentation is used cloning instead of bind or bindto)

$closure = function () {    echo "bind nothing.\n";};/ /With $bound_closure = Clone $closure; $bound_closure = Closure::bind ($closure, null); $bound _closure ();

  

Bind nothing.

This is the right clone.

How to understand closure bind and bindto in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.