Hướng dẫn can we pass function as a parameter in php? - chúng ta có thể chuyển hàm như một 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: //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: //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ự:

<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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

<?php /*create a sample function*/ function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; /*do the assignement*/ $obj->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)*/ ?>

Có, bạn có thể vượt qua một mảng một cách an toàn dưới dạng tham số.

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

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 ); 0

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

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.

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 1

Ví dụ #2 Danh sách đối số chức năng với dấu phẩy kéo dài

Vượt qua các đối số bằng cách tham khảo

Theo mặc định, các đối số hàm được truyền theo giá trị (để nếu giá trị của đối số trong hàm được thay đổi, nó không bị thay đổi bên ngoài hàm). Để cho phép một hàm sửa đổi các đối số của nó, chúng phải được truyền bằng cách tham chiếu.

Để có một đối số cho một hàm luôn được truyền bởi tham chiếu, hãy trả một ampersand và) cho tên đối số trong định nghĩa chức năng:

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 2

Ví dụ #3 Thông số chức năng chuyển qua tham chiếu

Đó là một lỗi để truyền một giá trị như là đối số được cho là được thông qua bởi tham chiếu.

Giá trị đối số mặc địnhcall_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 3 does not assign the default value.

Một hàm có thể xác định các giá trị mặc định cho các đối số bằng cách sử dụng cú pháp tương tự như gán một biến. Mặc định chỉ được sử dụng khi tham số không được chỉ định; Cụ thể, lưu ý rằng việc vượt qua call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 3 không gán giá trị mặc định.

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 4

Ví dụ #4 Sử dụng các tham số mặc định trong các chức năng

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

Ví dụ trên sẽ xuất ra: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.

Các giá trị tham số mặc định có thể là các giá trị vô hướng, mảng, loại đặc biệt call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 3 và kể từ Php 8.1.0, các đối tượng sử dụng cú pháp ClassName () mới.

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 6

Ví dụ #5 Sử dụng các loại không phân chia làm giá trị mặc định

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 7

Ví dụ #6 Sử dụng các đối tượng làm giá trị mặc định (kể từ Php 8.1.0)

Giá trị mặc định phải là một biểu thức không đổi, không (ví dụ) một biến, thành viên lớp hoặc lệnh gọi hàm.

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:

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ụ #4 Sử dụng các tham số mặc định trong các chức năng

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

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

Các giá trị tham số mặc định có thể là các giá trị vô hướng, mảng, loại đặc biệt call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 3 và kể từ Php 8.1.0, các đối tượng sử dụng cú pháp ClassName () mới.

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ụ #4 Sử dụng các tham số mặc định trong các chức năng

Making a bowl of raspberry yogurt.

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

Ví dụ #9 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.

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.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ụ #10 Khai báo các đối số tùy chọn sau các đối số bắt buộc

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

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

Lưu ý: Các đối số được truyền qua tham chiếu có thể có giá trị mặc định.: Arguments that are passed by reference may have a default value.

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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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.: 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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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.

Danh sách đối số có thể bao gồm mã thông báo <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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ụ:

Ví dụ #11 Sử dụng <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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

<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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

<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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:

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.array or Traversable variable or literal into the argument list:

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

<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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

<?php /*create a sample function*/ function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; /*do the assignement*/ $obj->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:

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.

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

<?php /*create a sample function*/ function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; /*do the assignement*/ $obj->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

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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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ụ:func_num_args(), func_get_arg() and func_get_args().

Ví dụ #11 Sử dụng <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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

<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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ố:

<?php /*create a sample function*/ function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; /*do the assignement*/ $obj->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

<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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:

Ví dụ #12 sử dụng <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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ố

Bạn có thể chỉ định các đối số vị trí bình thường trước mã thông báo <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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ố đó.

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

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

Cuối cùng, các đối số biến cũng có thể được truyền bằng cách tham chiếu bằng cách tiền tố <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->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 với một ampersand và (<?php /*create a sample function*/ function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; /*do the assignement*/ $obj->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)*/ ?> 8).

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

Các phiên bản cũ của PHP

Không có cú pháp đặc biệt nào được yêu cầu để lưu ý rằng một hàm là variadic; Tuy nhiên, quyền truy cập vào các đối số của hàm phải sử dụng func_num_args (), func_get_arg () và func_get_args ().

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

Ví dụ đầu tiên ở trên sẽ được thực hiện như sau trong các phiên bản cũ của PHP:

Ví dụ #14 Truy cập các đối số biến trong các phiên bản PHP cũ

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

Có tên là đối số

Php 8.0.0 được giới thiệu các đối số được đặt tên là một phần mở rộng của các tham số vị trí hiện có. Các đối số được đặt tên cho phép chuyển các đối số đến một hàm dựa trên tên tham số, thay vì vị trí tham số. Điều này làm cho ý nghĩa của việc tự ghi chép đối số, làm cho các đối số độc lập với thứ tự và cho phép bỏ qua các giá trị mặc định một cách tùy ý.

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

Các đối số được đặt tên được truyền bằng cách tiền tố giá trị với tên tham số theo sau là dấu hai chấm. Sử dụng các từ khóa dành riêng làm tên tham số được cho phép. Tên tham số phải là một định danh, chỉ định động không được phép.

Ví dụ #15 Cú pháp đối số được đặt tên

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

Ví dụ #16 Đối số vị trí so với các đối số được đặt tên

Thứ tự mà các đối số có tên được thông qua không quan trọng.

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

Ví dụ #17 Ví dụ tương tự như trên với một thứ tự khác nhau của các tham số

Các đối số được đặt tên có thể được kết hợp với các đối số vị trí. Trong trường hợp này, các đối số được đặt tên phải đến sau các đối số vị trí. Cũng có thể chỉ định một số đối số tùy chọn của một hàm, bất kể thứ tự của chúng.

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

Ví dụ #18 Kết hợp các đối số được đặt tên với các đối số vị trí

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 ¶

16 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

JCAPLAN tại Bogus Dot Amazon Dot Com ¶

16 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

Catman tại esteticas dot se ¶

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 ¶

16 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

Thông tin tại Keraweb dot nl ¶

Horst Schirmeier ¶

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

8 năm trước

Simmo ở 9000 chấm 000

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

7 tháng trước

Tesdy14 tại gmail dot com

call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 31

11 thàng trước

Twysto ¶

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

5 tháng trước

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 ¶

5 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

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

16 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

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 ); 50

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

Có một loại chức năng khác được gọi là các hàm tham số PHP, đây là các hàm có tham số được xác định trước.Bạn sẽ vượt qua bất kỳ số lượng tham số nào bên trong một hàm.Các tham số được truyền này hoạt động như các biến trong chức năng của bạn.Chúng được tuyên bố bên trong dấu ngoặc, sau tên hàm.You'll pass any number of parameters inside a function. These passed parameters act as variables in your function. They are declared inside the brackets, after the function name.

Bạn có thể chuyển trong một chức năng như một tham số?

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.

Tham số truyền trong PHP là gì?

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.

Chúng ta có thể chuyển mảng để hoạt động trong PHP không?

Có, bạn có thể vượt qua một mảng một cách an toàn dưới dạng tham số..

Chủ đề