����������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 ����ֵ��һ�������ķ�ʽ����ͨ������ֵ�����һ���ģ����ҲҪ���Ϸֺţ�
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($total, 2);;
}
}
$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.
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()��