Hướng dẫn anonymous function in php - chức năng ẩn danh trong php

Các hàm ẩn danh, còn được gọi là closures, cho phép tạo các chức năng không có tên được chỉ định. Chúng hữu ích nhất là giá trị của các tham số có thể gọi, nhưng chúng có nhiều cách sử dụng khác.callable parameters, but they have many other uses.

Các chức năng ẩn danh được thực hiện bằng cách sử dụng lớp đóng.Closure class.

Ví dụ #1 Chức năng ẩn danh

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>

Đóng cửa cũng có thể được sử dụng làm giá trị của các biến; PHP tự động chuyển đổi các biểu thức như vậy thành các phiên bản của lớp nội bộ đóng. Việc gán một đóng cho một biến sử dụng cùng một cú pháp như bất kỳ nhiệm vụ nào khác, bao gồm cả dấu chấm phẩy kéo dài:Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon:

Ví dụ #2 ví dụ biến chức năng ẩn danh

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>

Đóng cửa cũng có thể kế thừa các biến từ phạm vi cha mẹ. Bất kỳ biến nào như vậy phải được chuyển đến cấu trúc ngôn ngữ use. Kể từ Php 7.1, các biến này không được bao gồm các siêu thị, $ này hoặc các biến có cùng tên với tham số. Một khai báo loại trả về của hàm phải được đặt sau mệnh đề use.

Ví dụ #3 kế thừa các biến từ phạm vi cha mẹ

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

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"
string(11) "hello world"

Kể từ Php 8.0.0, danh sách các biến được sử dụng phạm vi có thể bao gồm dấu phẩy kéo dài, sẽ bị bỏ qua.

Việc kế thừa các biến từ phạm vi cha mẹ không giống như sử dụng các biến toàn cầu. Các biến toàn cầu tồn tại trong phạm vi toàn cầu, giống nhau cho dù chức năng nào đang thực thi. Phạm vi cha mẹ của việc đóng là hàm trong đó việc đóng được khai báo (không nhất thiết là hàm được gọi từ). Xem ví dụ sau:

Ví dụ #4 đóng cửa và phạm vi

// A basic shopping cart which contains a list of added products
// and the quantity of each product. Includes a method which
// calculates the total price of the items in the cart using a
// closure as a callback.
class Cart
{
    const 
PRICE_BUTTER  1.00;
    const 
PRICE_MILK    3.00;
    const 
PRICE_EGGS    6.95;

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

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

Ví dụ #5 Liên kết tự động của

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

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

Ví dụ trên sẽ xuất ra:

Khi được khai báo trong bối cảnh của một lớp, lớp hiện tại sẽ tự động bị ràng buộc với nó, cung cấp

Notice: Undefined variable: this in %s on line %d
NULL
2 có sẵn bên trong phạm vi của hàm. Nếu không liên kết tự động này của lớp hiện tại là không muốn, thì các hàm ẩn danh tĩnh có thể được sử dụng thay thế.

Chức năng ẩn danh tĩnh

Các chức năng ẩn danh có thể được khai báo tĩnh. Điều này ngăn họ khỏi lớp hiện tại tự động bị ràng buộc với họ. Các đối tượng cũng có thể không bị ràng buộc với chúng trong thời gian chạy.

Ví dụ #6 cố gắng sử dụng

Notice: Undefined variable: this in %s on line %d
NULL
2 bên trong hàm ẩn danh tĩnh

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

Ví dụ trên sẽ xuất ra:

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

Khi được khai báo trong bối cảnh của một lớp, lớp hiện tại sẽ tự động bị ràng buộc với nó, cung cấp

Notice: Undefined variable: this in %s on line %d
NULL
2 có sẵn bên trong phạm vi của hàm. Nếu không liên kết tự động này của lớp hiện tại là không muốn, thì các hàm ẩn danh tĩnh có thể được sử dụng thay thế.

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

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

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

Ví dụ trên sẽ xuất ra:

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

Khi được khai báo trong bối cảnh của một lớp, lớp hiện tại sẽ tự động bị ràng buộc với nó, cung cấp
Notice: Undefined variable: this in %s on line %d
NULL
2 có sẵn bên trong phạm vi của hàm. Nếu không liên kết tự động này của lớp hiện tại là không muốn, thì các hàm ẩn danh tĩnh có thể được sử dụng thay thế.

Chức năng ẩn danh tĩnh Các chức năng ẩn danh có thể được khai báo tĩnh. Điều này ngăn họ khỏi lớp hiện tại tự động bị ràng buộc với họ. Các đối tượng cũng có thể không bị ràng buộc với chúng trong thời gian chạy.
7.1.0 Ví dụ #6 cố gắng sử dụng
Notice: Undefined variable: this in %s on line %d
NULL
2 bên trong hàm ẩn danh tĩnh

Ví dụ #7 cố gắng liên kết một đối tượng với hàm ẩn danh tĩnh

Thay đổi

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

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

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

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

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

Phiên bản

Sự mô tả

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

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

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

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

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

Các hàm ẩn danh có thể không đóng qua các siêu thị, $ this hoặc bất kỳ biến nào có cùng tên với tham số.

orls ¶

closures0

closures1

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

12 năm trước

Dexen Dot Devries tại Gmail Dot Com ¶

closures3

4 năm trước

Sự mô tả

closures4

closures5

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

Các hàm ẩn danh có thể không đóng qua các siêu thị, $ this hoặc bất kỳ biến nào có cùng tên với tham số.

Thay đổi

closures7

closures8

closures9

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
0

Phiên bản

Sự mô tả

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
1

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
2

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
3

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
4

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
5

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
6

Các hàm ẩn danh có thể không đóng qua các siêu thị, $ this hoặc bất kỳ biến nào có cùng tên với tham số.

orls ¶

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
7

12 năm trước

Dexen Dot Devries tại Gmail Dot Com ¶

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
8

echo preg_replace_callback('~-([a-z])~', function ($match) {
    return 
strtoupper($match[1]);
}, 
'hello-world');
// outputs helloWorld
?>
9

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
0

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
1

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

4 năm trước

Chao ¶

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
3

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
4

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
5

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
6

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
7

$greet = function($name)
{
    
printf("Hello %s\r\n"$name);
};
$greet('World');
$greet('PHP');
?>
8

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

8 năm trước

orls ¶

use0

use1

use2

12 năm trước

Chao ¶

use3

use4

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

8 năm trước

orls ¶

use6

use7

use8

use9

use0

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

12 năm trước

Sự mô tả

use2

use3

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

Các hàm ẩn danh có thể không đóng qua các siêu thị, $ this hoặc bất kỳ biến nào có cùng tên với tham số.

Thay đổi

use5

use6

use7

use8

use9

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>
0

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>
1

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>
2

Phiên bản

Sự mô tả

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>
3

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>
4

$message 'hello';// No "use"
$example = function () {
    
var_dump($message);
};
$example();// Inherit $message
$example = function () use ($message) {
    
var_dump($message);
};
$example();// Inherited variable's value is from when the function
// is defined, not when called
$message 'world';
$example();// Reset message
$message 'hello';// Inherit by-reference
$example = function () use (&$message) {
    
var_dump($message);
};
$example();// The changed value in the parent scope
// is reflected inside the function call
$message 'world';
$example();// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    
var_dump($arg ' ' $message);
};
$example("hello");// Return type declaration comes after the use clause
$example = function () use ($message): string {
    return 
"hello $message";
};
var_dump($example());
?>
5