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.js
Incall
Orapply
, 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$this
Access Object. use the static member method directly.Class name: Member
Method.
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.