PHP two methods with the same name

前端 未结 4 950
借酒劲吻你
借酒劲吻你 2021-02-07 14:04

Can I have two methods sharing the same name, but with different arguments?

One would be public static and would take 2 arguments, the other one just public and takes on

相关标签:
4条回答
  • 2021-02-07 14:43

    No. PHP does not support classic overloading. (It does implement something else that is called overloading.)

    You can get the same result by using func_get_args() and it's related functions though:

    function ech()
    {
      $a = func_get_args();
      for( $t=0;$t<count($a); $t++ )
      {
        echo $a[$t];
      }
    }
    
    0 讨论(0)
  • 2021-02-07 14:50

    I'm just giving you the super lazy option:

    function __call($name, $args) {
        $name = $name . "_" . implode("_", array_map("gettype", $args)));
        return call_user_func_array(array($this, $name), $args);
    }
    

    That would for example invoke the real function name getPrice_string_array for two parameters of that type. That's sort of what languages with real method signature overloading support would do behind the scenes.

    Even lazier would be just counting the arguments:

    function __callStatic($name, $args) {
        $name = $name . "_" . count($args);
        return call_user_func_array(array($this, $name), $args);
    }
    

    That would invoke getPrice_1 for 1 argument, or getPrice_2 for, you guessed it, two arguments. This might already suffice for most use cases. Of course you can combine both alternatives, or make it more clever by search for all alternative real method names.

    If you want to keep your API pretty and user-friendly implementing such elaborate workarounds is acceptable. Very much so.

    0 讨论(0)
  • 2021-02-07 14:51

    PHP currently doesn't support overloading in known way, but you can still achieve your goal by using magic methods.

    From PHP5 manual: overloading.

    0 讨论(0)
  • 2021-02-07 14:54

    You could, kind of... I consider it very much "hack" solutions, but you could make a single function and assign a standard value, that wouldn't otherwise be okay to use, to the parameters as needed. Then if you do not pass the function a certain parameter, it will be set to fx "-1".

    public function getPrice($product_id = "-1", $currency) {
        if($product_id = "-1") {
            //do something
        }else {
            //do something
        }
    }
    

    Or if you really need one method to be static, you can make a method that evaluates which method to call and call that instead of your getPrice:

    public function whichGetPrice($product_id = "-1", $currency) {
        if($product !== "-1") {
            getStaticPrice($product_id, $currency);
        }else {
            getPrice($currency);
        }
    }
    

    Like I said, very much "hack" solutions. It's not exactly pretty, nor a way people would expect you to do it. So I wouldn't necessarily recommend it, but it can help you do what you want.

    0 讨论(0)
提交回复
热议问题