help with passing arguments to function

前端 未结 6 1118
慢半拍i
慢半拍i 2020-12-06 23:49
function get_tags_by_criteria($gender=\"%\", $min_age_of_birth=\"%\", $max_age_of_birth=\"%\", $country=\"%\", $region=\"%\", $city=\"%\", $tag=\"\") {
6条回答
  •  时光说笑
    2020-12-07 00:49

    You can simulate named arguments using an associative array:

    function my_function($options)
     {
      extract($options);
     }
    

    then call

    my_function(array("parameter1" => "value1", "parameter2" => "value2"));
    

    that, combined with robust checking and table of default values inside the function, works very nicely for me.

    Downside: There is no phpDoc convention to document the arguments, and your IDE will not be able to show the available arguments to you as you type. You will have to enter the available parameters into the @desc block which, depending on your IDE, may or may not look nice.

    One workaround for this is to declare all the parameters in the function, but make all of them but the first one optional. The first one can then be the associative array containing the values.

提交回复
热议问题