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

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化