<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] * * * * * ## 1 閉包(匿名)函數的意義 **閉包(匿名)函數通常作為簡單函數功能的實現。** 閉包(匿名)函數可以**賦值給變量**,或者**作為參數使用**。 閉包(匿名)函數是**函數編程**的基礎 ## 2 閉包(匿名)函數的使用 ### 2-1 匿名函數賦值給變量 ~~~ <?php $greet = function( $name ) { printf ( "Hello %s\r\n" , $name ); }; $greet ( 'World' ); $greet ( 'PHP' ); ?> ~~~ ### 2-2 匿名函數用作函數參數 ~~~ <?php echo preg_replace_callback ( '~-([a-z])~' , function ( $match ) { return strtoupper ( $match [ 1 ]); }, 'hello-world' ); ?> ~~~ 有沒有jQuery的post()函數感覺?? ## 3 閉包函數的父作用域參數傳遞 ### 3-1 顯式傳遞 作為function()的參數 ~~~ <?php $message = 'hello' ; $example = function ($message) { var_dump ( $message ); }; echo $example (); ?> ~~~ ### 3-2 隱式傳遞 使用use語言結構 ~~~ <?php $message = 'hello' ; $example = function () use ( $message ) { var_dump ( $message ); }; echo $example (); ?> ~~~ ### 3-3 引用參數 ~~~ <?php $message = 'hello' ; $example = function () use (& $message ) { var_dump ( $message ); }; echo $example (); ?> ~~~ ### 3-4 多參數傳遞 ~~~ <?php $message = 'world' ; $example = function ( $arg ) use ( $message ) { var_dump ( $arg . ' ' . $message ); }; $example ( "hello" ); ?> ~~~ ## 4 閉包函數的類作用域參數傳遞 閉包函數表達式在php底層實現為類\Closure的對象實例 **可以使用\Closure::bind()和$Closure->Bindto()** 修改閉包函數的參數和作用域為特定對象和類 ### 4-1 閉包對象方法: Closure::bindTo `public Closure Closure::bindTo ( object $newthis [, mixed $newscope = 'static' ] )` $newthis:參數對象 $newscope:參數作用域類 ~~~ <?php 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" ; ?> ~~~ 輸出: 1 2 ### 4-2 閉包類方法: Closure::bind `public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )` > $closure:需要修改的匿名函數對象 > $newthis:匿名函數的參數對象 > $newscope:匿名函數的參數類作用域 ~~~ <?php 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 >[info] 兩種不同在于一個是對象方法,一個是類靜態方法 ## 5 閉包函數的反射調用 閉包函數對象可以作為普通對象,使用反射api進行操作 如think\Route.php的Route::checkDomain() ~~~ if ($rule instanceof \Closure) { $reflect = new \ReflectionFunction($rule); self::$bind = $reflect->invokeArgs([]); return; } ~~~ ## 6 閉包函數的參數操作 ~~~ func_num_args() 獲取參數個數 func_get_arg() 獲取特定參數 func_get_args() 獲取參數數組 ~~~ 類似于js的arguments用法 ## 7 閉包函數的使用實例 ### 7-1 類中使用閉包函數 ~~~ <?php class Cart { const PRICE_BUTTER = 1.00 ; const PRICE_MILK = 3.00 ; const PRICE_EGGS = 6.95 ; protected $products = array(); public function add ( $product , $quantity ) { $this -> products [ $product ] = $quantity ; } public function getQuantity ( $product ) { return isset( $this -> products [ $product ]) ? $this -> products [ $product ] : FALSE ; } public function getTotal ( $tax ) { $total = 0.00 ; $callback = function ( $quantity , $product ) use ( $tax , & $total ) { $pricePerItem = constant ( __CLASS__ . "::PRICE_" . strtoupper ( $product )); $total += ( $pricePerItem * $quantity ) * ( $tax + 1.0 ); }; array_walk ( $this -> products , $callback ); return round ( $total , 2 );; } } $my_cart = new Cart ; $my_cart -> add ( 'butter' , 1 ); $my_cart -> add ( 'milk' , 3 ); $my_cart -> add ( 'eggs' , 6 ); print $my_cart -> getTotal ( 0.05 ) . "\n" ; ?> ~~~ ## 8 函數編程進階 ## 9 php的閉包函數與js閉包函數比較
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看