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){
...
}
}
I’m just giving you the super lazy option:
That would for example invoke the real function name
getPrice_string_arrayfor 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:
That would invoke
getPrice_1for 1 argument, orgetPrice_2for, 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.