How to use bind () and bindTo () of the PHPClosure class?

Source: Internet
Author: User
Check the usage of Closure bind and bindTo in the PHP Manual. I really don't understand the concept. For example, Closure: bind. in the manual, "copy a Closure and bind the specified $ this object and class scope ". How can this be understood? How is the Closure and $ this bound in the form of a... read the PHP manual about the usage of Closure bind and bindTo. I really don't understand the concept.
For example, Closure: bind. in the manual, "copy a Closure and bind the specified $ this object and class scope ". How can this be understood?
How is the closure and $ this bound together? What is the use of class scope?
Also, the static version and the dynamic version refer to the static version?
Can I explain these concepts with examples?


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

Thank you.

Reply content:

Check the usage of Closure bind and bindTo in the PHP Manual. I really don't understand the concept.
For example, Closure: bind. in the manual, "copy a Closure and bind the specified $ this object and class scope ". How can this be understood?
How is the closure and $ this bound together? What is the use of class scope?
Also, the static version and the dynamic version refer to the static version?
Can I explain these concepts with examples?


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

Thank you.

I wonder if you have used it.jsIncallOrapply, A bit similar.

Actually, it is to regard the closure as a member method or static member method of the object.

Closure: bind ($ cl1, null, 'A'); // It is equivalent to adding A static member method in the class Closure: bind ($ cl2, new A (), 'A'); // It is equivalent to adding A member method to the class.

Use in member methods$thisAccess Object. use the static member method directly.Class name: MemberMethod.
But because it is an anonymous function with no function name, a closure bound to the $ this object and class scope is returned for you to use.

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.