drlma06060 2014-07-16 15:34
浏览 24
已采纳

在PHP中调用函数的惯用方法是什么? [关闭]

I am new to PHP and codeigniter. Currently, whenever I write a model function, I pass to it multiple arguments rather than a single associative array like this:

function create_user($first_name, $last_name, $username)
{
    $this->db->insert('users', array('first_name'=>$first_name,
                                     'last_name'=>$last_name,
                                     'username'=>$username));
}

instead of this:

//where $user === Array('first_name'=>$first_name, 'last_name'=>$last_name, 'username'=>$username)
function create_user($user)
{
    $this->db->insert('users', $user);
}

I want to know which one is the idiomatic way. If both are fine and it's just a matter of personal preference, then I'd like to know which approach is more popular than the other.

Thank you.

  • 写回答

2条回答 默认 最新

  • dongyingdao8867 2014-07-16 18:46
    关注

    This is a general answer - it applies to pretty much every language.

    There is ONE exception (that springs to mind) - and that is any function that is part of an API should always take an array as this saves on work if you want to call a function from within your application or via a $_POST[] - you can use the same method with a couple of quick checks to see if it is a post via http or a variable passed in by the application itself (however very few people seem to be at the stage of consuming their own APIs yet :-P)

    Specific to Models and CI

    Always have named variables if you can - it will reduce the likelihood of errors in your application.

    Unless your application is massive / really data intensive (or has the potential to get that way in a matter of days) it is better to do multiple inserts one at a time through the model for debugging and shouldn't have any significant effect on speed.

    Additionally this generally makes the application more secure as you can define the variable types more easily (only numeric for example) this way and handle the error at controller level rather than model level

    General Answer

    It is very much dependant on the function - but naming variables should ALWAYS be the preference.

    I will try and explain when to use each:-

    1. Loads of optional parameters = array();

    If it is a function with loads of optional parameters then it is best to pass in an array - but I would say less than 20% of functions require this much flexibility. -> this saves on having to write

    private function myFunction($a, $b = "", $c = "", $d = "", $e = "", $f = "", $g = "", $h = "");
    
    $a = $this->myFunction("aa","","","","","","","bb");
    

    if you want to use just 2 parameters instead of

    private function myFunction($arr = array());
    
    $options = array("a"=>"aa","h"=>"bb");
    
    $a = $this->myFunction($options);
    

    Additionally if a function has that many parameters - it probably needs a re-write!

    2. Few optional parameters = named variables

    However - if it is a function that always requires 3 variables for example, you should hard code them in as that makes catching errors a whole lot easier (also with an IDE you should get code-hinting on your function if you comment it correctly.)

    The above example with an array means that when you have a function that MUST have at least 3 variables passed your IDE will have no idea that

    $options = array("a"=>"aa","h"=>"bb");
    
    $a = myFunction($options);
    

    Should throw an error.

    Additionally you have to write a lot of unnecessary checks to see that required variables are present and set.

    3. The Golden Rule and Middle Ground

    The BEST way to write functions - in nearly any language is:

    1. use named variables for REQUIRED fields.
    2. If you have optional values that always carry on from each other (so your first optional variable is necessary to need your second optional variable) then use $a = "", $b = ""
    3. For any other variables - or for functions that may take only one of 4/5 variables pass in an array.

    So your ideal scenario for a complex function would be

    private function _complex_function($required1, 
                                       $required2,
                                       $optionThatWillAlwaysBeRequiredBeforeOtherOptions = "",  
                                       $mixAndMatchOptions = array())
    {
    }
    

    Hope that is clear.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么