Hướng dẫn what is anonymous function give example in php? - Ví dụ về hàm ẩn danh trong php là gì?

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

Chức năng ẩn danh với ví dụ là gì?

Một hàm ẩn danh là một hàm được khai báo mà không có bất kỳ định danh có tên nào để chỉ nó. Như vậy, một hàm ẩn danh thường không thể truy cập được sau khi tạo ban đầu. Định nghĩa chức năng bình thường: function hello () {alert ('hello world'); } xin chào();a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();

Ý nghĩa của hàm ẩn danh là gì?

Các hàm ẩn danh thường là các đối số được truyền đến các hàm bậc cao hơn hoặc được sử dụng để xây dựng kết quả của hàm bậc cao hơn cần trả về một hàm.Nếu hàm chỉ được sử dụng một lần hoặc số lần giới hạn, hàm ẩn danh có thể nhẹ hơn so với sử dụng hàm được đặt tên.often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function.

Hàm ẩn danh được sử dụng ở đâu?

Hàm ẩn danh là một hàm không được lưu trữ trong tệp chương trình, nhưng được liên kết với một biến có kiểu dữ liệu là function_handle.Các hàm ẩn danh có thể chấp nhận nhiều đầu vào và trả về một đầu ra.Chúng chỉ có thể chứa một câu lệnh thực thi duy nhất.not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.

Tại sao một chức năng ẩn danh hữu ích?

Ưu điểm của một hàm ẩn danh là nó không phải được lưu trữ trong một tệp riêng biệt.Điều này có thể đơn giản hóa rất nhiều các chương trình, vì thường các tính toán rất đơn giản và việc sử dụng các hàm ẩn danh làm giảm số lượng tệp mã cần thiết cho một chương trình.it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.