Hướng dẫn php dynamic use - sử dụng động php

Cho dù câu hỏi kỳ lạ như thế nào (nó không phải là BTW), hãy để Lừa nghiêm túc trong giây lát! Nó có thể hữu ích khi có một lớp có thể khai báo các chức năng và biến chúng thành sự thật:

Được rồi, thử thách được chấp nhận!

Cho dù câu hỏi kỳ lạ như thế nào (nó không phải là BTW), hãy để Lừa nghiêm túc trong giây lát! Nó có thể hữu ích khi có một lớp có thể khai báo các chức năng và biến chúng thành sự thật:

"; });customFunctions::add("goodbye", // prepare function "goodbye" function($what,$when) { print "Goodbye cruel $what, "; print "I'm leaving you $when"; print "
"; });eval(customFunctions::make()); // inevitable - but it's safe!

Đó là nó! Bây giờ họ có chức năng thực sự. Không có $ -prefixing, không có đánh giá thời gian chạy bất cứ khi nào chúng được gọi-eval () chỉ cần một lần, để khai báo. Sau đó, họ hoạt động như bất kỳ chức năng nào.

Hãy để thử chúng:


Hướng dẫn php tạo chức năng động
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 

Phép thuật phía sau

Ở đây, lớp học có thể làm điều này. Thực sự không phải là một điều phức tạp:

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}

Nó cung cấp một bộ lưu trữ bên trong cho các chức năng của bạn, và sau đó khai báo các chức năng thực tế của người Hồi giáo gọi chúng. Đây là một cái gì đó tương tự như giải pháp Fardjad, nhưng với mã thực (không phải chuỗi) và do đó thuận tiện và dễ đọc hơn rất nhiều.

Các cuộc gọi lại có thể được biểu thị bằng khai báo loại có thể gọi được.callable type declaration. callable type declaration.


Hướng dẫn php tạo chức năng động

Phép thuật phía saucall_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods. call_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods.

Ở đây, lớp học có thể làm điều này. Thực sự không phải là một điều phức tạp:

Nó cung cấp một bộ lưu trữ bên trong cho các chức năng của bạn, và sau đó khai báo các chức năng thực tế của người Hồi giáo gọi chúng. Đây là một cái gì đó tương tự như giải pháp Fardjad, nhưng với mã thực (không phải chuỗi) và do đó thuận tiện và dễ đọc hơn rất nhiều.string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().

Các cuộc gọi lại có thể được biểu thị bằng khai báo loại có thể gọi được.callable type declaration. object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.


Hướng dẫn php tạo chức năng động

Phép thuật phía saucall_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods. object of that class by either, passing the class name instead of an object at index 0, or passing

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
9.

Ở đây, lớp học có thể làm điều này. Thực sự không phải là một điều phức tạp:

Nó cung cấp một bộ lưu trữ bên trong cho các chức năng của bạn, và sau đó khai báo các chức năng thực tế của người Hồi giáo gọi chúng. Đây là một cái gì đó tương tự như giải pháp Fardjad, nhưng với mã thực (không phải chuỗi) và do đó thuận tiện và dễ đọc hơn rất nhiều.:

Các cuộc gọi lại có thể được biểu thị bằng khai báo loại có thể gọi được.callable type declaration.

Hướng dẫn php tạo chức năng động

Phép thuật phía saucall_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods.

Nó cung cấp một bộ lưu trữ bên trong cho các chức năng của bạn, và sau đó khai báo các chức năng thực tế của người Hồi giáo gọi chúng. Đây là một cái gì đó tương tự như giải pháp Fardjad, nhưng với mã thực (không phải chuỗi) và do đó thuận tiện và dễ đọc hơn rất nhiều.string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().

Các cuộc gọi lại có thể được biểu thị bằng khai báo loại có thể gọi được.object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
0

Phép thuật phía sauobject of that class by either, passing the class name instead of an object at index 0, or passing

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
9.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
2

Nó cung cấp một bộ lưu trữ bên trong cho các chức năng của bạn, và sau đó khai báo các chức năng thực tế của người Hồi giáo gọi chúng. Đây là một cái gì đó tương tự như giải pháp Fardjad, nhưng với mã thực (không phải chuỗi) và do đó thuận tiện và dễ đọc hơn rất nhiều.:

Các cuộc gọi lại có thể được biểu thị bằng khai báo loại có thể gọi được.

Một số hàm như call_user_func () hoặc usort () chấp nhận các hàm gọi lại do người dùng xác định làm tham số. Các hàm gọi lại không chỉ có thể là các hàm đơn giản, mà còn các phương thức đối tượng, bao gồm các phương thức lớp tĩnh.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
1

Đi qua

Nó cung cấp một bộ lưu trữ bên trong cho các chức năng của bạn, và sau đó khai báo các chức năng thực tế của người Hồi giáo gọi chúng. Đây là một cái gì đó tương tự như giải pháp Fardjad, nhưng với mã thực (không phải chuỗi) và do đó thuận tiện và dễ đọc hơn rất nhiều.:

Các cuộc gọi lại có thể được biểu thị bằng khai báo loại có thể gọi được.callable type declaration. call_user_func() and call_user_func_array() will not be called if there is an uncaught exception thrown in a previous callback.

Hướng dẫn php tạo chức năng động

Đi qua

Một hàm PHP được truyền bởi tên của nó dưới dạng chuỗi. Bất kỳ hàm tích hợp hoặc định nghĩa người dùng nào cũng có thể được sử dụng, ngoại trừ các cấu trúc ngôn ngữ như: mảng (), echo, clan (), eval (), exit (), isset (), list (), in hoặc unset () .

Một phương thức của một đối tượng khởi tạo được truyền dưới dạng một mảng chứa một đối tượng tại INDEX 0 và tên phương thức tại INDEX 1. Truy cập các phương thức được bảo vệ và riêng tư từ trong một lớp được cho phép.

Các phương thức lớp tĩnh cũng có thể được truyền mà không cần khởi tạo một đối tượng của lớp đó bằng cách chuyển tên lớp thay vì một đối tượng tại INDEX 0 hoặc PASS

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
9.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

Ngoài chức năng do người dùng xác định phổ biến, các hàm ẩn danh và các hàm mũi tên cũng có thể được chuyển đến tham số gọi lại.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

Ngoài chức năng do người dùng xác định phổ biến, các hàm ẩn danh và các hàm mũi tên cũng có thể được chuyển đến tham số gọi lại.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
9

Ghi chú:

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
1

Kể từ Php 8.1.0, các hàm ẩn danh cũng có thể được tạo bằng cú pháp có thể gọi lớp đầu tiên. ¶

Nói chung, bất kỳ đối tượng thực hiện __invoke () cũng có thể được chuyển đến tham số gọi lại.

Ví dụ: Ví dụ: Ví dụ chức năng gọi lại

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
6

Ngoài chức năng do người dùng xác định phổ biến, các hàm ẩn danh và các hàm mũi tên cũng có thể được chuyển đến tham số gọi lại.

Ghi chú:

Kể từ Php 8.1.0, các hàm ẩn danh cũng có thể được tạo bằng cú pháp có thể gọi lớp đầu tiên. ¶

Nói chung, bất kỳ đối tượng thực hiện __invoke () cũng có thể được chuyển đến tham số gọi lại.

Ví dụ: Ví dụ: Ví dụ chức năng gọi lại

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
0

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
96

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

Nói chung, bất kỳ đối tượng thực hiện __invoke () cũng có thể được chuyển đến tham số gọi lại.

Ví dụ: Ví dụ: Ví dụ chức năng gọi lại

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
40

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
42

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
000

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
47

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
000

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
000

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
55

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
022

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
3
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
59

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
0

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
01

Các cuộc gọi lại được đăng ký với các chức năng như call_user_func () và call_user_func_array () sẽ không được gọi nếu có một ngoại lệ chưa được ném trong một cuộc gọi lại trước đó.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
03

Andrewbessa tại Gmail Dot Com ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
05

Andrewbessa tại Gmail Dot Com ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
07

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
2

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
09

10 năm trước ¶

Andrewbessa tại Gmail Dot Com ¶ ¶

Steve tại MrClay Dot org ¶

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
3

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
63

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
65

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
69

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
21

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
000

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7 ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
5

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
40

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
29

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
7 ¶

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

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
2
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
3
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6
class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
0

Các cuộc gọi lại được đăng ký với các chức năng như call_user_func () và call_user_func_array () sẽ không được gọi nếu có một ngoại lệ chưa được ném trong một cuộc gọi lại trước đó.

Andrewbessa tại Gmail Dot Com ¶

class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
722

10 năm trước ¶

Một phương thức của một đối tượng khởi tạo được truyền dưới dạng một mảng chứa một đối tượng tại INDEX 0 và tên phương thức tại INDEX 1. Truy cập các phương thức được bảo vệ và riêng tư từ trong một lớp được cho phép.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
03

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
05

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
07

Ghi chú:

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
09

Ví dụ #2 Ví dụ gọi lại bằng cách sử dụng đóng cửa

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
11

Nói chung, bất kỳ đối tượng thực hiện __invoke () cũng có thể được chuyển đến tham số gọi lại.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
13

Ví dụ: Ví dụ: Ví dụ chức năng gọi lại ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
022

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
3
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
4
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
6
class customFunctions { private static $store = []; private static $maker = ""; private static $declaration = ' function %s() { return call_user_func_array( %s::get(__FUNCTION__), func_get_args() ); } '; private static function safeName($name) { // extra safety against bad function names $name = preg_replace('/[^a-zA-Z0-9_]/',"",$name); $name = substr($name,0,64); return $name; } public static function add($name,$func) { // prepares a new function for make() $name = self::safeName($name); self::$store[$name] = $func; self::$maker.=sprintf(self::$declaration,$name,__CLASS__); } public static function get($name) {  // returns a stored callable return self::$store[$name]; } public static function make() {  // returns a string with all declarations return self::$maker; }}
0

Steve tại MrClay Dot org ¶

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
19

9 năm trước

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
21

Ngoài chức năng do người dùng xác định phổ biến, các hàm ẩn danh và các hàm mũi tên cũng có thể được chuyển đến tham số gọi lại.

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
23

Ghi chú:

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
25

9 năm trước

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
27

9 năm trước

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
29

Ngoài chức năng do người dùng xác định phổ biến, các hàm ẩn danh và các hàm mũi tên cũng có thể được chuyển đến tham số gọi lại.

Ghi chú:

 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
30
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
31
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
32
 hello('World'); // "Hello World" goodbye('world','today'); // "Goodbye cruel world, I'm leaving you today" 
33