Rumah > Artikel > pembangunan bahagian belakang > PHP7之Closure::call javascript closure need for closure google closur
Closure 类:匿名函数(在 PHP 5.3 中被引入)会产生这个类型的对象。
可将该类绑定到类或者对象上,即将自定义的方法动态添加到类或者对象上
php7之前使用的方法
Closure::bind :复制一个闭包,绑定指定的$this对象和类作用域。这个方法是 Closure::bindTo() 的静态版本
Closure::bindTo :复制当前闭包对象,绑定指定的$this对象和类作用域。创建并返回一个 匿名函数, 它与当前对象的函数体相同、绑定了同样变量,但可以绑定不同的对象,也可以绑定新的类作用域。
php7添加
<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>
<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>
<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>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了PHP7之Closure::call,包括了closure,php7方面的内容,希望对PHP教程有兴趣的朋友有所帮助。