Hướng dẫn php anonymous function() use multiple variables - hàm ẩn danh php () sử dụng nhiều biến

Php 5.3+ hỗ trợ các chức năng ẩn danh (mặc dù, nó hỗ trợ họ một chút khác nhau trong PHP 7.x+ về mặt ràng buộc). Tôi đang chạy php 5.6.x

Có một cú pháp cho phép nhiều đối số được chuyển đến một hàm ẩn danh (không cần sử dụng một mảng) được sử dụng làm cuộc gọi lại. Những ví dụ nào trong số này, nếu có, có thể trong PHP?callback. Which of these examples, if any, are possible in PHP?

ví dụ 1

function ($str1, $str2 ){   //But, that would be too easy, right?
    return $str1 . $str2;
}

Ví dụ 2

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}

Ví dụ 3

Chỉ vì tò mò, hình thức này có thể không?

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}

Tôi đã nhìn vào hướng dẫn sử dụng PHP, nhưng không thấy những gì tôi đang tìm kiếm.

Chức năng ẩn danh khác với hàm bình thường trong PHP như thế nào?callable parameters, but they have many other uses.

Các hàm ẩn danh tương tự như các chức năng thông thường, trong đó chúng chứa một khối mã được chạy khi chúng được gọi. Họ cũng có thể chấp nhận các đối số và trả về các giá trị. Sự khác biệt chính - như tên của chúng ngụ ý - là các hàm ẩn danh không có tên.Closure class.

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.

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

Các chức năng ẩn danh được thực hiện bằng cách sử dụng lớp đóng.Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon:

Ví dụ #1 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ể đượ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:

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

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
1

Đó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.

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"

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

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

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.

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
2

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
3

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
4

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:

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
6

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

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

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
5

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

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
5 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

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
9

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

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

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

function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
5

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
0

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
1

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
2

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

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

Ví dụ #5 Liên kết tự động của function () use ($string, $min, $max) { // Not seeing this in the manual. $length = mb_strlen($string, 'UTF-8'); return ($length >= $min) && ($length <= $max); } 5

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
function () use ($string, $min, $max) {  // Not seeing this in the manual.
    $length = mb_strlen($string, 'UTF-8');
    return ($length >= $min) && ($length <= $max);
}
5 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ế.
7.1.0 Chức năng ẩn danh tĩnh

orls ¶

12 năm trước

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
3

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
4

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
5

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
6

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Dexen Dot Devries tại Gmail Dot Com ¶

4 năm trước

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
8

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
9

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"
0

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"
1

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Chao ¶

8 năm trước

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"
3

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"
4

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Erolmon Dot Kskn tại Gmail Dot Com ¶

7 năm trước

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"
6

toonitw tại gmail dot com ¶

4 năm trước

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"
7

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"
8

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Chao ¶

12 năm trước

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

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

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

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

Dexen Dot Devries tại Gmail Dot Com ¶

4 năm trước

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

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

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

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

Chao ¶

8 năm trước

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

Erolmon Dot Kskn tại Gmail Dot Com ¶

7 năm trước

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

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

toonitw tại gmail dot com ¶

cướp tại Ubrio dot chúng tôi ¶

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
9

closures0

closures1

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Một schaffhirt dot tại sedna-soft dot de ¶

8 năm trước

closures3

closures4

closures5

Erolmon Dot Kskn tại Gmail Dot Com ¶

cướp tại Ubrio dot chúng tôi ¶

closures6

closures7

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Một schaffhirt dot tại sedna-soft dot de ¶

8 năm trước

closures9

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

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

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Erolmon Dot Kskn tại Gmail Dot Com ¶

4 năm trước

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

function ($str1, $str2 ) use ($int1, $int2) { // But, that would be in the manual?
    return $str1 . $str2 .' '. $int2 + $int2;
}
7

Chao ¶

12 năm trước

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

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

$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

8 năm trước

Erolmon Dot Kskn tại Gmail Dot Com ¶

$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

Làm thế nào bạn có thể chuyển một biến cục bộ cho một hàm ẩn danh trong PHP?

Có, sử dụng đóng cửa: functionName ($ someargument, function () sử dụng (& $ biến) {$ varable = "something";}); Lưu ý rằng để bạn có thể sửa đổi $ biến và truy xuất giá trị được sửa đổi bên ngoài phạm vi của hàm ẩn danh, nó phải được tham chiếu trong phần đóng bằng cách sử dụng &. Nó mới!use a closure: functionName($someArgument, function() use(&$variable) { $variable = "something"; }); Note that in order for you to be able to modify $variable and retrieve the modified value outside of the scope of the anonymous function, it must be referenced in the closure using & . It's new!

Các chức năng ẩn danh có thể có đối số?

Một hàm ẩn danh không thể truy cập được sau khi tạo ban đầu, nó chỉ có thể được truy cập bởi một biến mà nó được lưu trữ dưới dạng một hàm như một giá trị. Một hàm ẩn danh cũng có thể có nhiều đối số, nhưng chỉ có một biểu thức.An anonymous function can also have multiple arguments, but only one expression.

Hàm ẩn danh đưa ra ví dụ trong PHP là gì?

Các chức năng ẩn danh được thực hiện bằng cách sử dụng lớp đóng.Ví dụ #1 Chức năng ẩn danh ví dụ.echo preg_replace_callback ('~-([a-z]) ~', function ($ match) {return strtoupper ($ match [1]);}, 'Hello-world');implemented using the Closure class. Example #1 Anonymous function example. echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world');

Chức năng ẩn danh khác với hàm bình thường trong PHP như thế nào?

Các hàm ẩn danh tương tự như các chức năng thông thường, trong đó chúng chứa một khối mã được chạy khi chúng được gọi.Họ cũng có thể chấp nhận các đối số và trả về các giá trị.Sự khác biệt chính - như tên của chúng ngụ ý - là các hàm ẩn danh không có tên.anonymous functions have no name.