Hướng dẫn calls to php functions: - gọi đến các chức năng php:

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.

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

Đ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 () .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().

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

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 'ClassName::methodName'.object of that class by either, passing the class name instead of an object at index 0, or passing 'ClassName::methodName'.

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

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}

class

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>

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

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);

print

implode(' '$new_numbers);
?>

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

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.call_user_func() and call_user_func_array() will not be called if there is an uncaught exception thrown in a previous callback.

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

You can also use the $this variable to specify a callback:

class MyClass {

    public

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
0

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

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

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
1

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 ¶

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
2

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
3

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
4

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
5

10 năm trước

Steve tại MrClay Dot org ¶

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
6

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
7

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
8

// An example callback function
function my_callback_function() {
    echo 
'hello world!';
}
// An example callback method
class MyClass {
    static function 
myCallbackMethod() {
        echo 
'Hello World!';
    }
}
// Type 1: Simple callback
call_user_func('my_callback_function');// Type 2: Static class method call
call_user_func(array('MyClass''myCallbackMethod'));// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj'myCallbackMethod'));// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');// Type 5: Relative static class method call
class {
    public static function 
who() {
        echo 
"A\n";
    }
}
9

class0

class1

class2

class3

9 năm trước

Steve tại MrClay Dot org ¶

class4

class5

class6

class7

9 năm trước

Computrius tại gmail dot com

class8

class9

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
0

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
1

8 năm trước

Andrewbessa tại Gmail Dot Com ¶

10 năm trước

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
3

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
4

Steve tại MrClay Dot org ¶

9 năm trước

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
5

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
6

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
4

Computrius tại gmail dot com

9 năm trước

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
8

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
9

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
0

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
1

Computrius tại gmail dot com

8 năm trước

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
2

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
3

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
4

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
5

Riikka K ¶

7 năm trước

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
6

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
7

class6

// Our closure
$double = function($a) {
    return 
$a 2;
};
// This is our range of numbers
$numbers range(15);// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers array_map($double$numbers);
9

Edanschwartz tại Gmail Dot Com ¶

9 năm trước

print0

print1

print2

class6

print4

Computrius tại gmail dot com

8 năm trước

print5

print6

print7

print8

print9

implode(' '$new_numbers);
?>
0

implode(' '$new_numbers);
?>
1

Riikka K ¶

implode(' '$new_numbers);
?>
3

implode(' '$new_numbers);
?>
4

implode(' '$new_numbers);
?>
5

extends {
    public static function 
who() {
        echo 
"B\n";
    }
}
call_user_func(array('B''parent::who')); // A

// Type 6: Objects implementing __invoke can be used as callables

class {
    public function 
__invoke($name) {
        echo 
'Hello '$name"\n";
    }
}
$c = new C();
call_user_func($c'PHP!');
?>
4

7 năm trước

Edanschwartz tại Gmail Dot Com ¶

implode(' '$new_numbers);
?>
7

implode(' '$new_numbers);
?>
8

implode(' '$new_numbers);
?>
9

Invisiblesmiley ¶

8 năm trước

You can also use the $this variable to specify a callback: 0

You can also use the $this variable to specify a callback: 1

You can also use the $this variable to specify a callback: 2

You can also use the $this variable to specify a callback: 3

You can also use the $this variable to specify a callback: 4

You can also use the $this variable to specify a callback: 5

Riikka K ¶

7 năm trước

You can also use the $this variable to specify a callback: 6

You can also use the $this variable to specify a callback: 7

You can also use the $this variable to specify a callback: 8

You can also use the $this variable to specify a callback: 9

Edanschwartz tại Gmail Dot Com ¶