PHP7之Closure::call javascript closure need for closure google closur

WBOY
Release: 2016-07-29 08:52:16
Original
1486 people have browsed it

Closure class: Anonymous functions (introduced in PHP 5.3) produce objects of this type.
This class can be bound to a class or object, that is, custom methods are dynamically added to the class or object

Methods used before php7

  • Closure::bind: Copy a closure and bind the specified $this object and class scope. This method is the static version of Closure::bindTo()

  • Closure::bindTo: copies the current closure object and binds the specified $this object and class scope. Creates and returns an anonymous function, which has the same function body as the current object and binds the same variables, but can bind different objects or a new class scope.

php7 added

  • Closure::call(): Method was added as a temporary bound object scope to close and easily call its methods. Its performance is much faster than PHP5.6 bindTo.
<code><span>//bind.php</span><span><?php</span><span>/**
 * Created by PhpStorm.
 * User: bee
 * Date: 2016/4/24
 * Time: 22:35
 */</span><span><span>class</span><span>A</span> {</span><span>private</span><span>static</span><span>$sta</span> = <span>1</span>;
    <span>private</span><span>$com</span> = <span>2</span>;
}
<span>$cl1</span> = <span>static</span><span><span>function</span><span>()</span> {</span><span>return</span> A::<span>$sta</span>;
};
<span>$cl2</span> = <span><span>function</span><span>()</span> {</span><span>return</span><span>$this</span>->com;
};

<span>$bcl1</span> = Closure::bind(<span>$cl1</span>, <span>null</span>, <span>'A'</span>);
<span>$bcl2</span> = Closure::bind(<span>$cl2</span>, <span>new</span> A(), <span>'A'</span>);
<span>echo</span><span>$bcl1</span>(), <span>"\n"</span>;
<span>echo</span><span>$bcl2</span>(), <span>"\n"</span>;</span></code>
Copy after login
<code><span><?php</span><span>//bindTo.php</span><span>/**
 * Created by PhpStorm.
 * User: bee
 * Date: 2016/4/24
 * Time: 22:35
 */</span><span><span>class</span><span>A</span> {</span><span><span>function</span><span>__construct</span><span>(<span>$val</span>)</span> {</span><span>$this</span>->val = <span>$val</span>;
    }
    <span><span>function</span><span>getClosure</span><span>()</span> {</span><span>//returns closure bound to this object and scope</span><span>return</span><span><span>function</span><span>()</span> {</span><span>return</span><span>$this</span>->val; };
    }
}

<span>$ob1</span> = <span>new</span> A(<span>1</span>);
<span>$ob2</span> = <span>new</span> A(<span>2</span>);

<span>$cl</span> = <span>$ob1</span>->getClosure();
<span>echo</span><span>$cl</span>(), <span>"\n"</span>;

<span>$add</span> = <span><span>function</span><span>()</span>{</span><span>return</span><span>$this</span>->val+<span>1</span>;
};
<span>$cl</span> = <span>$add</span>->bindTo(<span>$ob2</span>);
<span>//与call相比,需要增加()方可被调用</span><span>echo</span><span>$cl</span>(), <span>"\n"</span>;</span></code>
Copy after login
<code><span><?php</span><span>//call.php</span><span>/**
 * Created by PhpStorm.
 * User: bee
 * Date: 2016/4/24
 * Time: 22:35
 */</span><span><span>class</span><span>Value</span> {</span><span>protected</span><span>$value</span>;

    <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$value</span>)</span> {</span><span>$this</span>->value = <span>$value</span>;
    }

    <span>public</span><span><span>function</span><span>getValue</span><span>()</span> {</span><span>return</span><span>$this</span>->value;
    }
}

<span>$three</span> = <span>new</span> Value(<span>3</span>);
<span>$four</span> = <span>new</span> Value(<span>4</span>);

<span>$closure</span> = <span><span>function</span><span>(<span>$delta</span>)</span> {</span><span>return</span><span>$this</span>->getValue() + <span>$delta</span>; };
<span>//可直接调用,不用在后面增加()</span><span>echo</span><span>$closure</span>->call(<span>$three</span>, <span>3</span>);
<span>echo</span><span>$closure</span>->call(<span>$four</span>, <span>4</span>);
</span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced Closure::call of PHP7, including closure and php7. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!