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à 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
Đó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
Đó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ữ 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ư: 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
Notice: Undefined variable: this in %s on line %d NULL0 Notice: Undefined variable: this in %s on line %d NULL1 Ví dụ #5 Liên kết tự động của Notice: Undefined variable: this in %s on line %d NULL3 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 NULL2 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ĩnhCá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 NULL6 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 NULL7 Notice: Undefined variable: this in %s on line %d NULL8 Notice: Undefined variable: this in %s on line %d NULL9 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
|
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 NULL2 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 %d0
Warning: Cannot bind an instance to a static closure in %s on line %d1
Warning: Cannot bind an instance to a static closure in %s on line %d2
Warning: Cannot bind an instance to a static closure in %s on line %d3
Warning: Cannot bind an instance to a static closure in %s on line %d4
Phiên bản ¶
Sự mô tả
Warning: Cannot bind an instance to a static closure in %s on line %d5
Warning: Cannot bind an instance to a static closure in %s on line %d6
Warning: Cannot bind an instance to a static closure in %s on line %d7
Warning: Cannot bind an instance to a static closure in %s on line %d8
Warning: Cannot bind an instance to a static closure in %s on line %d4
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 ¶
closures
0
closures
1
Warning: Cannot bind an instance to a static closure in %s on line %d4
12 năm trước ¶
Dexen Dot Devries tại Gmail Dot Com ¶
closures
3
4 năm trước ¶
Sự mô tả
closures
4
closures
5
Warning: Cannot bind an instance to a static closure in %s on line %d4
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
closures
7
closures
8
closures
9
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
0Phiên bản ¶
Sự mô tả
echo preg_replace_callback('~-([a-z])~', function ($match) {
1
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
echo preg_replace_callback('~-([a-z])~', function ($match) {
2
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
echo preg_replace_callback('~-([a-z])~', function ($match) {
3
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
echo preg_replace_callback('~-([a-z])~', function ($match) {
4
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
echo preg_replace_callback('~-([a-z])~', function ($match) {
5
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
6Cá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) {
7
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
12 năm trước ¶
Dexen Dot Devries tại Gmail Dot Com ¶
echo preg_replace_callback('~-([a-z])~', function ($match) {
8
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
echo preg_replace_callback('~-([a-z])~', function ($match) {
9
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
$greet = function($name)
0
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
$greet = function($name)
1
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
Warning: Cannot bind an instance to a static closure in %s on line %d4
4 năm trước ¶
Chao ¶
$greet = function($name)
3
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
$greet = function($name)
4
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
$greet = function($name)
5
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
$greet = function($name)
6
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
$greet = function($name)
7
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
$greet = function($name)
8
{
printf("Hello %s\r\n", $name);
};$greet('World');
$greet('PHP');
?>
Warning: Cannot bind an instance to a static closure in %s on line %d4
8 năm trước ¶
orls ¶
use
0
use
1
use
212 năm trước ¶
Chao ¶
use
3
use
4
Warning: Cannot bind an instance to a static closure in %s on line %d4
8 năm trước ¶
orls ¶
use
6
use
7
use
8
use
9
use
0
Warning: Cannot bind an instance to a static closure in %s on line %d4
12 năm trước ¶
Sự mô tả
use
2
use
3
Warning: Cannot bind an instance to a static closure in %s on line %d4
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
use
5
use
6
use
7
use
8
use
9
$message = 'hello';// No "use"
0
$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());
?>
$message = 'hello';// No "use"
1
$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());
?>
$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());
?>
2Phiên bản ¶
Sự mô tả
$message = 'hello';// No "use"
3
$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());
?>
$message = 'hello';// No "use"
4
$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());
?>
$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