PHP7之Closure::call javascript 閉包 需要閉包 google closur

WBOY
發布: 2016-07-29 08:52:16
原創
1487 人瀏覽過

Closure 類別:匿名函數(在 PHP 5.3 中被引入)會產生這個類型的物件。
可將該類別綁定到類別或物件上,即將自訂的方法動態新增至類別或物件上

php7之前使用的方法

  • Closure::bind :複製一個閉包,綁定指定的$this物件和類別作用域。這個方法是 Closure::bindTo() 的靜態版本

  • Closure::bindTo :複製目前閉包對象,綁定指定的$this物件和類別作用域。建立並傳回一個 匿名函數, 它與目前物件的函數體相同、綁定了相同變量,但可以綁定不同的對象,也可以綁定新的類別作用域。

php7新增

  • Closure::call() : 方法被加入作為臨時綁定的物件範圍,以封閉並簡單地呼叫它的方法。它的性能相比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>
登入後複製
<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教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!