PHP two methods with the same name

十年热恋 提交于 2021-02-17 21:29:57

问题


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 only one argument

example

class product{

  protected
    $product_id;

  public function __construct($product_id){
    $this->product_id = $product_id;
  }

  public static function getPrice($product_id, $currency){
    ...
  }

  public function getPrice($currency){
    ...
  }

}

回答1:


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.




回答2:


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];
  }
}



回答3:


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

From PHP5 manual: overloading.




回答4:


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.



来源:https://stackoverflow.com/questions/8409204/php-two-methods-with-the-same-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!