php函數總結(閉包函數,匿名函數)

php函數總結php

1.普通函數html

2.變量函數閉包

function myfun($a) { echo $a; } $b = "myfun"; $b("test");

 

3.匿名函數(能夠實現閉包)函數

  匿名函數(Anonymous functions),也叫閉包函數(Closures),容許臨時建立一個沒有指定名稱的函數,常常用做回調函數(callback)的參數,固然也有其餘應用狀況oop

$func = function() { };//要帶分號
$func() //調用
var_dump($func); //返回對象類型 object(Closure)#1 (0) { }

 

4.閉包函數:將匿名函數在普通函數中當作參數出入,也能夠被返回,就實現了一個簡單的閉包.this

  通俗的說,子函數可使用父函數中的局部變量,這種行爲就叫作閉包.spa

  閉包的特色:.net

    1.做爲一個函數變量的一個引用,當函數返回時,其處於激活狀態.code

    2.一個閉包就是當一個函數返回時,一個沒有釋放資源的棧區htm

--其實上面兩點能夠合成一點,就是閉包函數返回時,該函數內部變量處於激活狀態,函數所在棧區依然保留.

function myfunc() { $a=10; $b=11; $one = function($str)use(&$a,$b){//use引用外層變量 不加&傳副本不影響父函數值
        echo $a=$a+2; echo '<br/>'; echo $b=$b+2; echo '<br/>'; echo $str; }; echo $a; echo '---<br/>'; echo $b; echo '---<br/>'; return $one; } $a = myfunc(); $a('你好');

 

父函數中把匿名函數做爲返回值返回,閉包的一種..

 

 5.內部函數

 

 

擴展知識php:USE關鍵詞的用法

1.命名空間

2.閉包函數上下文

3.Trait代碼複用時 引用....  (參考http://php.net/manual/zh/language.oop5.traits.php)

 

Closure 類

  • Closure::__construct — 用於禁止實例化的構造函數
  • Closure::bind — 複製一個閉包,綁定指定的$this對象和類做用域。
  • Closure::bindTo — 複製當前閉包對象,綁定指定的$this對象和類做用域。

 

Closure::bind

(PHP 5 >= 5.4.0, PHP 7)

Closure::bind — 複製一個閉包,綁定指定的$this對象和類做用域。(深入理解)

說明

public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )

這個方法是 Closure::bindTo() 的靜態版本。查看它的文檔獲取更多信息。

參數

closure

須要綁定的匿名函數。

newthis

須要綁定到匿名函數的對象,或者 NULL 建立未綁定的閉包。

newscope

想要綁定給閉包的類做用域,或者 'static' 表示不改變。若是傳入一個對象,則使用這個對象的類型名。 類做用域用來決定在閉包中 $this 對象的 私有、保護方法 的可見性。 The class scope to which associate the closure is to be associated, or 'static' to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object.

返回值

返回一個新的 Closure 對象 或者在失敗時返回 FALSE

範例

class A { private static $sfoo = 1; private $ifoo = 2; } $cl1 = static function() { return A::$sfoo; }; $cl2 = function() { return $this->ifoo; }; $bcl1 = Closure::bind($cl1, null, 'A'); $bcl2 = Closure::bind($cl2, new A(), 'A'); echo $bcl1(), "\n"; echo $bcl2(), "\n";

以上例程的輸出相似於:

1
2

參見

 

 

Closure::bindTo

(PHP 5 >= 5.4.0, PHP 7)

Closure::bindTo — 複製當前閉包對象,綁定指定的$this對象和類做用域

說明

public Closure Closure::bindTo ( object $newthis [, mixed $newscope = 'static' ] )

 

建立並返回一個 匿名函數, 它與當前對象的函數體相同、綁定了一樣變量,但能夠綁定不一樣的對象,也能夠綁定新的類做用域。

「綁定的對象」決定了函數體中的 $this 的取值,「類做用域」表明一個類型、決定在這個匿名函數中可以調用哪些 私有 和 保護 的方法。 也就是說,此時 $this 能夠調用的方法,與 newscope 類的成員函數是相同的。

靜態閉包不能有綁定的對象( newthis 參數的值應該設爲 NULL)不過仍然能夠用 bubdTo 方法來改變它們的類做用域。

This function will ensure that for a non-static closure, having a bound instance will imply being scoped and vice-versa. To this end, non-static closures that are given a scope but a NULL instance are made static and non-static non-scoped closures that are given a non-null instance are scoped to an unspecified class.

Note:

若是你只是想要複製一個匿名函數,能夠用 cloning 代替。

參數

newthis

綁定給匿名函數的一個對象,或者 NULL 來取消綁定。

newscope

關聯到匿名函數的類做用域,或者 'static' 保持當前狀態。若是是一個對象,則使用這個對象的類型爲心得類做用域。 這會決定綁定的對象的 保護、私有成員 方法的可見性。

返回值

返回新建立的 Closure 對象 或者在失敗時返回 FALSE

範例

Example #1 Closure::bindTo() 實例

class A { function __construct($val) { $this->val = $val; } function getClosure() { //returns closure bound to this object and scope
        return function() { return $this->val; }; } } $ob1 = new A(1); $ob2 = new A(2); $cl = $ob1->getClosure(); echo $cl(), "\n"; $cl = $cl->bindTo($ob2); echo $cl(), "\n";
class A{ private  $aa = ''; } $fun = function () { $this->aa = "31"; }; $cl = $fun->bindTo(new A(),'A'); $cl();

 

 

http://php.net/manual/zh/class.closure.php 

http://php.net/manual/zh/closure.bindto.php

 

上一篇文章:http://www.cnblogs.com/fps2tao/p/8727248.html 

相關文章
相關標籤/搜索