Hướng dẫn can you pass functions as parameters in php? - bạn có thể chuyển các hàm dưới dạng tham số trong php không?

Được kiểm tra cho PHP 5.3

Như tôi thấy ở đây, chức năng ẩn danh có thể giúp bạn: http://php.net/manual/en/funces.anonymous.php

Những gì có thể bạn sẽ cần và nó không được nói trước đó là cách truyền một chức năng mà không gói nó vào trong một chức năng được tạo ra trên đường. Như bạn sẽ thấy sau này, bạn sẽ cần chuyển tên của hàm được viết trong một chuỗi dưới dạng tham số, hãy kiểm tra "khả năng gọi" của nó và sau đó gọi nó.

Chức năng cần kiểm tra:

if( is_callable( $string_function_name ) ){
    /*perform the call*/
}

Sau đó, để gọi nó, sử dụng đoạn mã này (nếu bạn cũng cần tham số, hãy đặt chúng vào một mảng), được xem tại: http://php.net/manual/en/function.call-user-func.php

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );

như nó theo sau (theo một cách tương tự, không tham số):

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");

Đây là một lớp giải thích cách làm điều gì đó tương tự:

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>

và một ví dụ về việc sử dụng


         
hello to
justPrintSomething = 'sayHello'; /*note that the given "sayHello" it's a string ! */ /*now call it*/ $obj->justPrintSomething(); /*will print: "hello to all" and a break-line, for html purpose*/ /*if the string assigned is not denoting a defined method , it's treat as a simple value*/ $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL'; echo $obj->justPrintSomething; /*what do you expect to print? just that string*/ /*N.B.: "justPrintSomething" is treated as a variable now! as the __set 's override specify"*/ /*after the assignement, the what is the function's destiny assigned before ? It still works, because it's held on a different array*/ $obj->justPrintSomething("Jack Sparrow"); /*You can use that "variable", ie "justPrintSomething", in both ways !! so you can call "justPrintSomething" passing itself as a parameter*/ $obj->justPrintSomething( $obj->justPrintSomething ); /*prints: "hello to thisFunctionJustNotExistsLOL" and a break-line*/ /*in fact, "justPrintSomething" it's a name used to identify both a value (into the dictionary of values) or a function-name (into the dictionary of functions)*/ ?>

rsperduta tại gmail dot com ¶

igorsantos07 tại gmail dot com ¶

John ¶

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
0

Dmitry Dot Balabka tại Gmail Dot Com ¶

Shaman_master tại danh sách dot ru ¶

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
1

2 năm trước

Chúng ta có thể chuyển chức năng như một tham số trong PHP không?

Các hàm tham số PHP là các hàm với các tham số. Bạn có thể vượt qua bất kỳ số lượng tham số bên trong một hàm. Các tham số được truyền này hoạt động như các biến bên trong chức năng của bạn. Chúng được chỉ định bên trong dấu ngoặc đơn, sau tên hàm.

Bạn có thể vượt qua các chức năng dưới dạng tham số không?

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
2

Chuyển một hàm dưới dạng tham số cho một hàm khác C ++ có hai cách để truyền một hàm dưới dạng tham số. Như bạn thấy, bạn có thể sử dụng Hoạt động () hoặc hoạt động2 () để cho kết quả tương tự.

Bạn có thể vượt qua một chức năng trong PHP không?

Thông tin đối số chức năng PHP có thể được truyền đến các chức năng thông qua các đối số. Một đối số giống như một biến. Đối số được chỉ định sau tên hàm, bên trong dấu ngoặc đơn. Bạn có thể thêm nhiều đối số như bạn muốn, chỉ cần tách chúng bằng dấu phẩy.

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
3 does not assign the default value.

Có bao nhiêu cách bạn có thể chuyển tham số cho một hàm trong PHP?

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
4

Có hai cách khác nhau để chuyển các tham số cho một hàm. Đầu tiên, và phổ biến hơn, là theo giá trị. Cái khác là bằng cách tham khảo.

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.

Thông tin có thể được truyền đến các chức năng thông qua danh sách đối số, đây là danh sách các biểu thức được phân phối bằng dấu phẩy. Các đối số được đánh giá từ trái sang phải, trước khi hàm thực sự được gọi (đánh giá háo hức).arrays, the special type

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
3, and as of PHP 8.1.0, objects using the new ClassName() syntax.

PHP hỗ trợ các đối số truyền theo giá trị (mặc định), đi qua tham chiếu và giá trị đối số mặc định. Danh sách đối số có độ dài thay đổi và các đối số được đặt tên cũng được hỗ trợ.

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
6

Ví dụ số 1 chuyển mảng cho các chức năng

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
7

Kể từ Php 8.0.0, danh sách các đối số chức năng có thể bao gồm dấu phẩy kéo dài, sẽ bị bỏ qua. Điều đó đặc biệt hữu ích trong trường hợp danh sách các đối số dài hoặc chứa các tên biến dài, làm cho nó thuận tiện để liệt kê các đối số theo chiều dọc.

Lưu ý rằng bất kỳ đối số tùy chọn nên được chỉ định sau bất kỳ đối số bắt buộc nào, nếu không chúng không thể được bỏ qua khỏi các cuộc gọi. Xem xét ví dụ sau:

Ví dụ #7 Sử dụng không chính xác đối số chức năng mặc định

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
8

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
9

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
0

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

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42

Bây giờ, so sánh những điều trên với điều này:

Ví dụ #8 Sử dụng đúng đối số chức năng mặc định

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
1

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
9

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
3

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

Making a bowl of raspberry yogurt.

Bây giờ, so sánh những điều trên với điều này:

Ví dụ #8 Sử dụng đúng đối số chức năng mặc định

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
4

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
5

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
6

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

Making a bowl of raspberry natural yogurt.

Bây giờ, so sánh những điều trên với điều này:

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
3 default makes the type implicitly nullable. This usage remains allowed, though it is recommended to use an explicit nullable type instead.

Ví dụ #8 Sử dụng đúng đối số chức năng mặc định

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
9

Kể từ Php 8.0.0, các đối số được đặt tên có thể được sử dụng để bỏ qua nhiều tham số tùy chọn.: As of PHP 7.1.0, omitting a parameter which does not specify a default throws an ArgumentCountError; in previous versions it raised a Warning.

Ví dụ #9 Sử dụng đúng đối số chức năng mặc định: Arguments that are passed by reference may have a default value.

Kể từ Php 8.0.0, tuyên bố các đối số bắt buộc sau khi các đối số tùy chọn bị phản đối. Điều này thường có thể được giải quyết bằng cách bỏ giá trị mặc định, vì nó sẽ không bao giờ được sử dụng. Một ngoại lệ cho quy tắc này là các đối số của Mẫu function funToBeCalled(){ print("----------------------i'm here"); } function wrapCaller($fun){ if( is_callable($fun)){ print("called"); call_user_func($fun); }else{ print($fun." not called"); } } wrapCaller("funToBeCalled"); wrapCaller("cannot call me"); 7, trong đó mặc định call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 3 làm cho loại này không thể vượt qua được. Việc sử dụng này vẫn được phép, mặc dù nên sử dụng một loại có thể điều chỉnh rõ ràng.

Ví dụ #10 Khai báo các đối số tùy chọn sau các đối số bắt buộc

Lưu ý: Kể từ Php 7.1.0, việc bỏ qua một tham số không chỉ định một mặc định ném một archarchChoolror; Trong các phiên bản trước, nó đã nêu ra một cảnh báo.: It is also possible to achieve variable-length arguments by using func_num_args(), func_get_arg(), and func_get_args() functions. This technique is not recommended as it was used prior to the introduction of the

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0 token.

Lưu ý: Các đối số được truyền qua tham chiếu có thể có giá trị mặc định.

Danh sách đối số có độ dài thay đổi

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
4

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
5

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
6

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

Bây giờ, so sánh những điều trên với điều này:array or Traversable variable or literal into the argument list:

Ví dụ #8 Sử dụng đúng đối số chức năng mặc định

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
9

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
5


         
hello to
justPrintSomething = 'sayHello'; /*note that the given "sayHello" it's a string ! */ /*now call it*/ $obj->justPrintSomething(); /*will print: "hello to all" and a break-line, for html purpose*/ /*if the string assigned is not denoting a defined method , it's treat as a simple value*/ $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL'; echo $obj->justPrintSomething; /*what do you expect to print? just that string*/ /*N.B.: "justPrintSomething" is treated as a variable now! as the __set 's override specify"*/ /*after the assignement, the what is the function's destiny assigned before ? It still works, because it's held on a different array*/ $obj->justPrintSomething("Jack Sparrow"); /*You can use that "variable", ie "justPrintSomething", in both ways !! so you can call "justPrintSomething" passing itself as a parameter*/ $obj->justPrintSomething( $obj->justPrintSomething ); /*prints: "hello to thisFunctionJustNotExistsLOL" and a break-line*/ /*in fact, "justPrintSomething" it's a name used to identify both a value (into the dictionary of values) or a function-name (into the dictionary of functions)*/ ?>
1

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

Bây giờ, so sánh những điều trên với điều này:

Ví dụ #8 Sử dụng đúng đối số chức năng mặc định

Kể từ Php 8.0.0, các đối số được đặt tên có thể được sử dụng để bỏ qua nhiều tham số tùy chọn.


         
hello to
justPrintSomething = 'sayHello'; /*note that the given "sayHello" it's a string ! */ /*now call it*/ $obj->justPrintSomething(); /*will print: "hello to all" and a break-line, for html purpose*/ /*if the string assigned is not denoting a defined method , it's treat as a simple value*/ $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL'; echo $obj->justPrintSomething; /*what do you expect to print? just that string*/ /*N.B.: "justPrintSomething" is treated as a variable now! as the __set 's override specify"*/ /*after the assignement, the what is the function's destiny assigned before ? It still works, because it's held on a different array*/ $obj->justPrintSomething("Jack Sparrow"); /*You can use that "variable", ie "justPrintSomething", in both ways !! so you can call "justPrintSomething" passing itself as a parameter*/ $obj->justPrintSomething( $obj->justPrintSomething ); /*prints: "hello to thisFunctionJustNotExistsLOL" and a break-line*/ /*in fact, "justPrintSomething" it's a name used to identify both a value (into the dictionary of values) or a function-name (into the dictionary of functions)*/ ?>
6

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

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2

Ví dụ #9 Sử dụng đúng đối số chức năng mặc định

Kể từ Php 8.0.0, tuyên bố các đối số bắt buộc sau khi các đối số tùy chọn bị phản đối. Điều này thường có thể được giải quyết bằng cách bỏ giá trị mặc định, vì nó sẽ không bao giờ được sử dụng. Một ngoại lệ cho quy tắc này là các đối số của Mẫu
    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
7, trong đó mặc định
call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
3 làm cho loại này không thể vượt qua được. Việc sử dụng này vẫn được phép, mặc dù nên sử dụng một loại có thể điều chỉnh rõ ràng.

Ví dụ #10 Khai báo các đối số tùy chọn sau các đối số bắt buộcfunc_num_args(), func_get_arg() and func_get_args().

Lưu ý: Kể từ Php 7.1.0, việc bỏ qua một tham số không chỉ định một mặc định ném một archarchChoolror; Trong các phiên bản trước, nó đã nêu ra một cảnh báo.

Lưu ý: Các đối số được truyền qua tham chiếu có thể có giá trị mặc định.


         
hello to
justPrintSomething = 'sayHello'; /*note that the given "sayHello" it's a string ! */ /*now call it*/ $obj->justPrintSomething(); /*will print: "hello to all" and a break-line, for html purpose*/ /*if the string assigned is not denoting a defined method , it's treat as a simple value*/ $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL'; echo $obj->justPrintSomething; /*what do you expect to print? just that string*/ /*N.B.: "justPrintSomething" is treated as a variable now! as the __set 's override specify"*/ /*after the assignement, the what is the function's destiny assigned before ? It still works, because it's held on a different array*/ $obj->justPrintSomething("Jack Sparrow"); /*You can use that "variable", ie "justPrintSomething", in both ways !! so you can call "justPrintSomething" passing itself as a parameter*/ $obj->justPrintSomething( $obj->justPrintSomething ); /*prints: "hello to thisFunctionJustNotExistsLOL" and a break-line*/ /*in fact, "justPrintSomething" it's a name used to identify both a value (into the dictionary of values) or a function-name (into the dictionary of functions)*/ ?>
9

    function funToBeCalled(){
        print("----------------------i'm here");
    }
    function wrapCaller($fun){
        if( is_callable($fun)){
            print("called");
            call_user_func($fun);
        }else{
            print($fun." not called");
        }
    }

    wrapCaller("funToBeCalled");
    wrapCaller("cannot call me");
5

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
6

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

Danh sách đối số có độ dài thay đổi

PHP có hỗ trợ cho các danh sách đối số có độ dài thay đổi trong các chức năng do người dùng xác định bằng cách sử dụng mã thông báo

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0.

Lưu ý: Cũng có thể đạt được các đối số có độ dài thay đổi bằng cách sử dụng các hàm func_num_args (), func_get_arg () và func_get_args (). Kỹ thuật này không được khuyến nghị vì nó đã được sử dụng trước khi giới thiệu mã thông báo

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0.

Danh sách đối số có thể bao gồm mã thông báo

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0 để biểu thị rằng hàm chấp nhận một số lượng đối số biến. Các đối số sẽ được chuyển vào biến đã cho dưới dạng một mảng; Ví dụ:

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
2

Ví dụ #11 Sử dụng

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0 để truy cập các đối số biến

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
3

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0 cũng có thể được sử dụng khi gọi các chức năng để giải nén một mảng hoặc biến có thể đi qua hoặc nghĩa đen vào danh sách đối số:

Ví dụ #12 sử dụng

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0 để cung cấp các đối số

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
4

Bạn có thể chỉ định các đối số vị trí bình thường trước mã thông báo

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0. Trong trường hợp này, chỉ các đối số kéo dài không khớp với đối số vị trí sẽ được thêm vào mảng được tạo bởi
functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0.

Cũng có thể thêm một khai báo loại trước mã thông báo

functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0. Nếu điều này có mặt, thì tất cả các đối số được ghi lại bởi
functions[$name] = $data;
        else
            $this->vars[$name] = $data;
    }

    function __get($name){
        $t = $this->vars[$name];
        if(isset($t))
            return $t;
        else{
            $t = $this->$functions[$name];
            if( isset($t))
                return $t;
        }
    }

    function __call($method,$args=null){
        $fun = $this->functions[$method];
        if(isset($fun)){
            call_user_func_array($fun,$args);
        } else {
            // error out
            print("ERROR: Funciton not found: ". $method);
        }
    }
}
?>
0 phải khớp với loại tham số đó.

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
5

Ví dụ #13 Loại đối số biến được khai báo

Ví dụ #19 Lỗi ném khi vượt qua cùng một tham số nhiều lần

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
6

Kể từ Php 8.1.0, có thể sử dụng các đối số được đặt tên sau khi giải nén các đối số. Một đối số được đặt tên không được ghi đè một đối số đã được giải nén.

Ví dụ #20 Sử dụng các đối số được đặt tên sau khi giải nén

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
7

PHP tại Richardneill Dot org ¶

7 năm trước

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
8

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
9

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
0

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
1

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
2

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
3

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
4

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
5

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
6

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
7

Lilywhite ¶

1 năm trước

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
8

Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
9

Making a bowl of raspberry yogurt.
0

Making a bowl of raspberry yogurt.
1

Gabriel tại FigDice Dot org ¶

6 năm trước

Making a bowl of raspberry yogurt.
2

Making a bowl of raspberry yogurt.
3

Making a bowl of raspberry yogurt.
4

Making a bowl of raspberry yogurt.
5

Boan Dot Web tại Outlook Dot Com ¶

4 năm trước

Making a bowl of raspberry yogurt.
6

Making a bowl of raspberry yogurt.
7

Making a bowl of raspberry yogurt.
8

Making a bowl of raspberry yogurt.
9

Making a bowl of raspberry natural yogurt.
0

Hayley Watson ¶

5 năm trước

Making a bowl of raspberry natural yogurt.
1

Making a bowl of raspberry natural yogurt.
2

Making a bowl of raspberry natural yogurt.
3

Making a bowl of raspberry natural yogurt.
4

Sergio Santana: Ssantana tại tlaloc dot imta dot mx ¶

17 năm trước

Making a bowl of raspberry natural yogurt.
5

Making a bowl of raspberry natural yogurt.
6

Making a bowl of raspberry natural yogurt.
7

Making a bowl of raspberry natural yogurt.
8

Making a bowl of raspberry natural yogurt.
9

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
0

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
1

Making a bowl of raspberry natural yogurt.
0

Catman tại esteticas dot se ¶

6 năm trước

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
3

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
4

Making a bowl of raspberry natural yogurt.
0

Boan Dot Web tại Outlook Dot Com ¶

4 năm trước

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
6

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
7

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
8

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
9

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
00

Making a bowl of raspberry yogurt.
4

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
02

Hayley Watson ¶

5 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
03

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
04

Making a bowl of raspberry natural yogurt.
0

Hayley Watson ¶

5 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
06

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
07

Making a bowl of raspberry natural yogurt.
0

Sergio Santana: Ssantana tại tlaloc dot imta dot mx ¶

17 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
09

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
10

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
11

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
12

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
13

Making a bowl of raspberry natural yogurt.
0

Catman tại esteticas dot se ¶

JCAPLAN tại Bogus Dot Amazon Dot Com ¶

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
15

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
16

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
17

16 năm trước

1 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
18

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
19

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
20

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
21

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
22

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
23

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
24

Making a bowl of raspberry natural yogurt.
0

Gabriel tại FigDice Dot org ¶

6 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
26

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
27

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
28

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
29

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
30

Boan Dot Web tại Outlook Dot Com ¶

4 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
31

Hayley Watson ¶

1 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
32

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
33

Making a bowl of raspberry natural yogurt.
0

Gabriel tại FigDice Dot org ¶

4 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
35

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
36

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
37

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
38

Making a bowl of raspberry natural yogurt.
0

Hayley Watson ¶

4 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
40

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
41

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
42

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
43

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
44

Making a bowl of raspberry natural yogurt.
0

Hayley Watson ¶

4 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
46

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
47

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
48

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
49

Hayley Watson ¶

5 năm trước

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
50

Chúng ta có thể chuyển chức năng như một tham số trong PHP không?

Các hàm tham số PHP là các hàm với các tham số. Bạn có thể vượt qua bất kỳ số lượng tham số bên trong một hàm. Các tham số được truyền này hoạt động như các biến bên trong chức năng của bạn. Chúng được chỉ định bên trong dấu ngoặc đơn, sau tên hàm.You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name.

Bạn có thể vượt qua các chức năng dưới dạng tham số không?

Chuyển một hàm dưới dạng tham số cho một hàm khác C ++ có hai cách để truyền một hàm dưới dạng tham số.Như bạn thấy, bạn có thể sử dụng Hoạt động () hoặc hoạt động2 () để cho kết quả tương tự.C++ has two ways to pass a function as a parameter. As you see, you can use either operation() or operation2() to give the same result.

Bạn có thể vượt qua một chức năng trong PHP không?

Thông tin đối số chức năng PHP có thể được truyền đến các chức năng thông qua các đối số.Một đối số giống như một biến.Đối số được chỉ định sau tên hàm, bên trong dấu ngoặc đơn.Bạn có thể thêm nhiều đối số như bạn muốn, chỉ cần tách chúng bằng dấu phẩy.Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

Có bao nhiêu cách bạn có thể chuyển tham số cho một hàm trong PHP?

Có hai cách khác nhau để chuyển các tham số cho một hàm.Đầu tiên, và phổ biến hơn, là theo giá trị.Cái khác là bằng cách tham khảo.two different ways of passing parameters to a function. The first, and more common, is by value. The other is by reference.