Closure class: Anonymous functions (introduced in PHP 5.3) produce objects of this type.
You can bind the class to a class or object and add a custom method to the class or object dynamically
Methods used prior to PHP7
Closure::bind: Copies a closure that binds the specified $this object and class scope. This method is a static version of Closure::bindto ()
Closure::bindto: Copies the current closure object, binding the specified $this object and class scope. Creates and returns an anonymous function that is the same as the current object's function body, binds the same variable, but can bind different objects, or bind a new class scope.
PHP7 add
- Closure::call (): Method is added as a temporary bound object scope to enclose and simply invoke its method. Its performance is much faster than PHP5.6 bindto.
//bind.php
/** * Created by PhpStorm. * User: bee * Date: 2016/4/24 * Time: 22:35 */classA {privatestatic$sta = 1; private$com = 2;}$cl1 = staticfunction() {return A::$sta;};$cl2 = function() {return$this->com;};$bcl1 = Closure::bind($cl1, null, 'A');$bcl2 = Closure::bind($cl2, new A(), 'A');echo$bcl1(), "\n";echo$bcl2(), "\n";
//bindto.php/** * Created by Phpstorm. * User:bee * date:2016/4/24 * time:22:35 * * classA { function__construct($val) {$this->val =$val; } functiongetclosure() {//returns closure bound to this object and scopereturn function() {return$this->val; }; }}$ob 1=NewA1);$ob 2=NewA2);$CL=$ob 1->getclosure ();Echo$CL(),"\ n";$add= function(){return$this->val+1;};$CL=$add->bindto ($ob 2);() need to be increased () in order to be called when compared to callEcho$CL(),"\ n";
//call.php/** * Created by Phpstorm. * User:bee * date:2016/4/24 * time:22:35 * * classValue {protected$value; Public function__construct($value) {$this->value =$value; } Public functiongetValue() {return$this->value; }}$three=NewValue (3);$four=NewValue (4);$closure= function($delta) {return$this->getvalue () +$delta; };//Can be called directly, not added at the back ()Echo$closure->call ($three,3);Echo$closure->call ($four,4);
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the PHP7 of the Closure::call, including the CLOSURE,PHP7 aspects of the content, I hope that the PHP tutorial interested in a friend helpful.