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>
以上就介紹了PHP7之Closure::call,包括了closure,php7方面的內容,希望對PHP教學有興趣的朋友有幫助。