4

In Java, we have a method overloading feature that's very useful for Singletons. For example, i have two different getInstance methods, public static, that behave differently based on the parameters received:

public static Currency getInstance(String currencyCode)
public static Currency getInstance(Locale locale)

Can we do this in PHP?

hakre
  • 193,403
  • 52
  • 435
  • 836
Jomezxn
  • 51
  • 2
  • 6
    In short: No. (This answer is too short even for a comment.) – deceze Jul 05 '11 at 12:23
  • possible duplicate of [What is PHP function overloading for?](http://stackoverflow.com/questions/1512295/what-is-php-function-overloading-for) – Felix Kling Jul 05 '11 at 12:26

3 Answers3

3

You can determine the argument type at run-time:

function getInstance($currency) {
   if (is_string($currency)) {
      $currency = lookupLokale($currency);
   }
   // do something with the $currency object
}

In php5.3+ (php5.0+ for non-static methods), you can also use php's method overloading to implement Java-like semantics yourself. However, OOP overloading is likely to produce messy code, and you should prefer the above in-method solution.

In most cases, it's clearer if you just use two different method names.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • I understand the hacks to get the same behavior, i'm using them. I just thought that maybe there was a better way. – Jomezxn Jul 05 '11 at 12:27
  • 2
    @Jomezxn You're asking the wrong question when you're trying to map language-specific concepts from one language to another. – phihag Jul 05 '11 at 12:30
1

Come on, at least try to Google :). Theres excellent documentation about this. For example on the PHP site ITSELF:

EDIT: New link that describes method overloading

http://www.dinke.net/blog/en/2007/08/01/method-overloading-in-php5/

Now I got the same kind of overloading.

sinni800
  • 1,451
  • 14
  • 29
  • 1
    That's not quite the same kind of overloading as the OP wants though. – deceze Jul 05 '11 at 12:25
  • The other alternative is to use `func_get_args()` and then call the fitting implementations based on their types. However not that specific as in Java or .Net. – hakre Jul 05 '11 at 12:26
  • I really meant overloading as in having multiple functions with the same name but different parameters. I just read the page and php just refers to something different on the same name... OOP in PHP is just so broken... EDIT: Found an internet post that discusses the correct kind of overloading. – sinni800 Jul 05 '11 at 12:30
0

Directly PHP don't supports method overloading, but we can implement this feature with func_get_args():

class Obj
{
    function __construct() {

        $args = func_get_args();

        if (count($args) != 2) {
            echo ("Must be passed two arguments !\n");
            return;
        }

        if (is_numeric($args[0]) && is_numeric($args[1])) {
            $result = $this->_addnum($args[0], $args[1]);
        }
        else if (is_string($args[0]) && is_string($args[1])) {
            $result = $this->_addstring($args[0], $args[1]);
        }
        else if (is_bool($args[0]) && is_bool($args[1])) {
            $result = $this->_addbool($args[0], $args[1]);
        }
        else if (is_array($args[0]) && is_array($args[1])) {
            $result = $this->_addarray($args[0], $args[1]);
        }
        else {
            echo ("Argument(s) type is not supported !\n");
            return;
        }

        echo "\n";
        var_dump($result);
    }

    private function _addnum($x, $y) {return $x + $y;}

    private function _addstring($x, $y) {return $x . $y;}

    private function _addbool($x, $y) {return $x xor $y;}

    private function _addarray($x, $y) {return array_merge($x,$y);}
}

// invalid constructor cases
new Obj();
new Obj(null, null);

// valid ones
new Obj(2,3);
new Obj('A','B');
new Obj(false, true);
new Obj([3], [4]);

Outputs :

Must be passed two arguments !
Argument(s) type is not supported !

int(5)
string(2) "AB"
bool(true)
array(2) {
  [0] =>
  int(3)
  [1] =>
  int(4)
}
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70