dongpao5658 2010-07-20 15:26
浏览 40
已采纳

重载参数类型有哪些问题/好处?

Often I write functions whose operation depends depends on the type of some argument. An example would be a function that would help me to insert rows into a mysql database.

function insert($table, $columns, $values) {
    if(is_string($values)) {
        $values = "('" . $values . "')";
        }
    else if(is_array($values)) {
        $values = "('" . implode("','", $values) . "')";
        }

    //process $table and $columns here

    mysql_query("INSERT INTO $table $columns $values");
    }

The idea is that sometimes I will only want to insert a single value but sometimes I will want to insert an array of values. However, this means that the type of $values will depend on the situation and I notice that in general, the built-in PHP functions tend to avoid this. I feel like if the PHP core implemented such a function, it would instead be split into insertString and insertArray to handle both cases. The benefits of overloading the type are that the user doesn't need to worry about what they put into $values as long as it is a string or an array. For example, if they forget that they can pass in a string, they can still pass in array('singleValue') without any problems. Therefore, the using the function becomes easier. Are there any pitfalls with this approach?

Furthermore, we could extend our function to insert multiple rows at once. These would be input as a multidimensional array and so would need a line that processed them as follows:

if(is_multi($values)) {
    foreach($values as $key => $value) {
        $values[$key] = "('" . implode("','", $value) . "')";
        }
    $values = implode(",", $values);
    }

Again we could either tack this on to the original function or create a new function, insertMulti. The difference between this case and the earlier case is that technically the type of $values doesn't change between insertArray and insertMulti. However, the way in which we handle $values does change and this change is dependent on the structure of $values. So if there are benefits and pitfalls to type overloading, do they apply to this situation as well or should this be treated differently?

Edit: Thank you for your answers! I realise that there are many PHP functions that accept mixed values but there are also many that do not. For example, strlen and count seem to me to be analogous functions except that one handles strings and the other handles arrays and objects. Would it be better then to implement countAll which handles strings, arrays and objects? Are they only kept separate for historical reasons?

  • 写回答

2条回答 默认 最新

  • doumen1883 2010-07-20 15:44
    关注

    Not only does PHP have core functions that allow this, it is common for popular PHP frameworks like CakePHP to supply methods that accept such mixed arguments.

    The only thing to watch out for is if your function behaves in an exceptional way when an empty value is passed to the function (perhaps, returning all records or something). Ensure that type of behavior is consistent among related functions and documented. Otherwise, callers inadvertently passing an empty array (perhaps the results of another function) could be in for a surprise.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法