php Lamba&Closures

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Lamba&Closures相关的知识,希望对你有一定的参考价值。

<?php

function getMultiplier($product)
{
	echo "kolikrat se spusti?"; // jen 1
	
    return function ($value) use ($product) {
        return $value * $product;
    };
}

// $multi23 je "násobička 23"
$multi23 = getMultiplier(23);
echo $multi23(1);
echo $multi23(2);
echo $multi23(3); 
echo $multi23(4);
<?php

class Dispatcher {
    //Added explicit callback declaration.
    var $callback;

    public function Dispatcher( $callback ){
         $this->callback = $callback;
    }

    public function asynchronous_method(){
       //do asynch stuff, like fwrite...then, fire callback.
       if ( isset( $this->callback ) ) {
            if (function_exists( $this->callback )) call_user_func( $this->callback, "File done!" );
        }
    }

}

d = new Dispatcher( 'do_callback' );
$d->asynchronous_method();

function do_callback( $data ){
   print 'Data is: ' .  $data .  "\n";
}
<?php

// This function uses a callback function. 
function doIt($callback) 
{ 
    $data = "this is my data";
    $callback($data); 
} 


// This is a sample callback function for doIt(). 
function myCallback($data) 
{ 
    print 'Data is: ' .  $data .  "\n"; 
} 


// Call doIt() and pass our sample callback function's name. 
doIt('myCallback');
<?php

function iWillReturnCallback($callBackHere){
    $data = array('title' => 'Romes', 'result' => 'OK');
	return $callBackHere($data);
}

function iAmCallBack($data){
	return '<b>'.$data['result'].'</b>';
}

echo iWillReturnCallback(function($data){
	return '<b>'.$data['result'].'</b>';
});


$luckyDip = array(
 
  function() {
    echo "You got a bag of toffees!";
  },
 
  function() {
    echo "You got a toy car!";
  },
 
  function() {
    echo "You got some balloons!";
  }
);

$choice = rand( 0, 2 );
$luckyDip[$choice]();
<?php


$people = array(
  array( "name" => "Fred", "age" => 39 ),
  array( "name" => "Sally", "age" => 23 ),
  array( "name" => "Mary", "age" => 46 )
);
 




function getSortFunction( $sortKey ) {
  return function( $personA, $personB ) use ( $sortKey ) {
    return ( $personA[$sortKey] < $personB[$sortKey] ) ? -1 : 1;
  };
}

// closures
 
echo "Sorted by name:<br><br>";
usort( $people, getSortFunction( "name" ) );
print_r( $people );
echo "<br>";
 
echo "Sorted by age:<br><br>";
usort( $people, getSortFunction( "age" ) );
print_r( $people );
echo "<br>";


// classic way
usort( $people, function( $personA, $personB ) {
  return ( $personA["age"] < $personB["age"] ) ? -1 : 1;
} );
<?php

class FileUpload
{
    function save($callback)
    {
        $path = '/path/image';
         
        echo "Data saved<br />";
        //call the callback
        $callback($path);
    }
}

class Profile
{
    function update_profile()
    {
        $f = new FileUpload('photo');
         
        // Note, this syntax is very similar to javascript callback syntax
    $f->save(function($path){
        echo "Image resized at path $path <br />";    
    });
     
    }
}
 
$p = new Profile;
$p->update_profile();

以上是关于php Lamba&Closures的主要内容,如果未能解决你的问题,请参考以下文章

什么是 PHP 中的 Closures/Lambda 或 Javascript 中的外行术语? [复制]

[R] Lexical & Dynamic Scoping / Execution & Calling environments / Closures

You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)

You Don't Know JS: Scope & Closures (第4章: Hoisting)

You Don't Know JS: Scope & Closures (第3章: 函数 vs 块作用域)

大数据架构+Lamba+Kappa+Unifield