PHP7 closure::call JavaScript closure need for closure Google Closur

Source: Internet
Author: User

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.

  • 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.