锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷Anonymous functions锟斤拷锟斤拷也锟叫闭帮拷锟斤拷锟斤拷锟斤拷closures锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷时锟斤拷锟斤拷一锟斤拷没锟斤拷指锟斤拷锟斤拷锟狡的猴拷锟斤拷锟斤拷锟筋经锟斤拷锟斤拷锟斤拷锟截碉拷锟斤拷锟斤拷锟斤拷callback锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷然锟斤拷也锟斤拷锟斤拷锟斤拷应锟矫碉拷锟斤拷锟斤拷锟�

锟斤拷锟斤拷锟斤拷锟斤拷目前锟斤拷通锟斤拷 Closure 锟斤拷锟斤拷实锟街的★拷

Example #1 锟斤拷锟斤拷锟斤拷锟斤拷示锟斤拷

<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// 锟斤拷锟� helloWorld
?>

锟秸帮拷锟斤拷锟斤拷也锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷值锟斤拷使锟矫★拷PHP 锟斤拷锟皆讹拷锟窖达拷锟街憋拷锟绞阶拷锟斤拷锟斤拷锟斤拷锟斤拷锟� Closure 锟侥讹拷锟斤拷实锟斤拷锟斤拷锟斤拷一锟斤拷 closure 锟斤拷锟斤拷值锟斤拷一锟斤拷锟斤拷锟斤拷锟侥凤拷式锟斤拷锟斤拷通锟斤拷锟斤拷锟斤拷值锟斤拷锟斤法锟斤拷一锟斤拷锟侥o拷锟斤拷锟揭惨拷锟斤拷戏趾牛锟�

Example #2 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值示锟斤拷

<?php
$greet 
= function($name)
{
    
printf("Hello %s\r\n"$name);
};

$greet('World');
$greet('PHP');
?>

锟秸帮拷锟斤拷锟皆从革拷锟斤拷锟斤拷锟斤拷锟叫继承憋拷锟斤拷锟斤拷 锟轿何达拷锟斤拷锟斤拷锟斤拷锟接︼拷锟斤拷锟� use 锟斤拷锟皆结构锟斤拷锟捷斤拷去锟斤拷 PHP 7.1 锟金,诧拷锟杰达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 superglobals锟斤拷 $this 锟斤拷锟竭和诧拷锟斤拷锟斤拷锟斤拷锟斤拷

Example #3 锟接革拷锟斤拷锟斤拷锟斤拷坛斜锟斤拷锟�

<?php
$message 
'hello';

// 没锟斤拷 "use"
$example = function () {
    
var_dump($message);
};
echo 
$example();

// 锟教筹拷 $message
$example = function () use ($message) {
    
var_dump($message);
};
echo 
$example();

// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
echo 
$example();

// Reset message
$message 'hello';

// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
echo 
$example();

// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
echo 
$example();

// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");
?>

锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�

Notice: Undefined variable: message in /example.php on line 6
NULL
string(5) "hello"
string(5) "hello"
string(5) "hello"
string(5) "world"
string(11) "hello world"

锟斤拷些锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节猴拷锟斤拷锟斤拷锟斤拷锟酵凤拷锟斤拷锟斤拷锟斤拷锟� 锟接革拷锟斤拷锟斤拷锟斤拷锟叫继承憋拷锟斤拷锟斤拷使锟斤拷全锟街憋拷锟斤拷锟斤拷锟斤拷同锟侥★拷全锟街憋拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷全锟街的凤拷围锟斤拷锟斤拷锟桔碉拷前锟斤拷执锟叫碉拷锟斤拷锟侥革拷锟斤拷锟斤拷锟斤拷锟斤拷 锟秸帮拷锟侥革拷锟斤拷锟斤拷锟斤拷锟角讹拷锟斤拷帽瞻锟斤拷暮锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷堑锟斤拷锟斤拷锟斤拷暮锟斤拷锟斤拷锟斤拷锟绞撅拷锟斤拷锟斤拷拢锟�

Example #4 Closures 锟斤拷锟斤拷锟斤拷锟斤拷

<?php
// 一锟斤拷锟斤拷锟斤拷锟侥癸拷锟斤车锟斤拷锟斤拷锟斤拷一些锟窖撅拷锟斤拷拥锟斤拷锟狡凤拷锟矫匡拷锟斤拷锟狡凤拷锟斤拷锟斤拷锟斤拷锟�
// 锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟姐购锟斤车锟斤拷锟斤拷锟斤拷锟斤拷品锟斤拷锟杰价格,该凤拷锟斤拷使
// 锟斤拷锟斤拷一锟斤拷 closure 锟斤拷为锟截碉拷锟斤拷锟斤拷锟斤拷
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($total2);;
    }
}

$my_cart = new Cart;

// 锟斤拷锟斤拷锟斤车锟斤拷锟斤拷锟斤拷锟侥�
$my_cart->add('butter'1);
$my_cart->add('milk'3);
$my_cart->add('eggs'6);

// 锟斤拷锟斤拷锟斤拷芗鄹锟斤拷锟斤拷锟斤拷锟� 5% 锟斤拷锟斤拷锟斤拷税.
print $my_cart->getTotal(0.05) . "\n";
// 锟斤拷锟斤拷锟斤拷锟� 54.29
?>

Example #5 Automatic binding of $this

<?php

class Test
{
    public function 
testing()
    {
        return function() {
            
var_dump($this);
        };
    }
}

$object = new Test;
$function $object->testing();
$function();
    
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

object(Test)#1 (0) {
}

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷PHP 5.3锟叫碉拷锟斤拷锟斤拷锟�

Notice: Undefined variable: this in script.php on line 8
NULL

As of PHP 5.4.0, when declared in the context of a class, the current class is automatically bound to it, making $this available inside of the function's scope. If this automatic binding of the current class is not wanted, then static anonymous functions may be used instead.

Static anonymous functions

As of PHP 5.4, anonymous functions may be declared statically. This prevents them from having the current class automatically bound to them. Objects may also not be bound to them at runtime.

Example #6 Attempting to use $this inside a static anonymous function

<?php

class Foo
{
    function 
__construct()
    {
        
$func = static function() {
            
var_dump($this);
        };
        
$func();
    }
};
new 
Foo();

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Notice: Undefined variable: this in %s on line %d
NULL

Example #7 Attempting to bind an object to a static anonymous function

<?php

$func 
= static function() {
    
// function body
};
$func $func->bindTo(new StdClass);
$func();

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Warning: Cannot bind an instance to a static closure in %s on line %d

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
7.1.0 Anonymous functions may not close over superglobals, $this, or any variable with the same name as a parameter.
5.4.0 $this 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
5.3.0 锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

注锟斤拷

Note: 锟斤拷锟斤拷锟节闭帮拷锟斤拷使锟斤拷 func_num_args()锟斤拷func_get_arg() 锟斤拷 func_get_args()锟斤拷