tags:

views:

856

answers:

5

I started off OOP with Java, and now I'm getting pretty heavy into PHP. Is it possible to create multiples of a function with different arguments like in Java? Or will the interpreted / untyped nature of the language prevent this and cause conflicts?

A: 

No this isn't possible, because PHP can't defer from the arguments which function you want (you don't specify which types you expect). You can however give default values to arguments in php.

That way the caller can give different amounts of arguments. This will call the same function though.

Example is:

function test($a = true)

This gives a default of true if 0 arguments are given, and takes the calling value if 1 argument is given.

Thirler
+1  A: 

Short answer: No. There can only be one function with a given name.

Longer answer: You can do this by creating a convoluted include system that includes the function with the right number of arguments. Or, better yet, you can take advantage of PHP allowing default values for parameters and also a variable amount of parameters.

To take advantage of default values just assign a value to a parameter when defining the function:

function do_something($param1, $param2, $param3 = 'defaultvaule') {}

It's common practice to put parameters with default values at the end of the function declaration since they may be omitted when the function is called and makes the syntax for using them clearer:

do_something('value1', 'value2'); // $param3 is 'defaultvaule' by default

You can also send a variable amount of parameters by using func_num_args() and func_get_arg() to get the arguments:

<?php
  function dynamic_args() {
      echo "Number of arguments: " . func_num_args() . "<br />";
      for($i = 0 ; $i < func_num_args(); $i++) {
          echo "Argument $i = " . func_get_arg($i) . "<br />";
      }
  }

  dynamic_args("a", "b", "c", "d", "e");
?>
John Conde
+1  A: 

Following isn't possible with php

function funcX($a){
    echo $a;
}
function funcX($a,$b){
    echo $a.$b;
}

Instead do this way

function funcX($a,$b=2){
    echo $a.$b;
}

funcX(1) will display 12, func(1,3) will display 13

S.Mark
+3  A: 

Everyone else has answers with good code explanations. Here is an explanation in more high level terms: Java supports Method overloading which is what you are referring to when you talk about function with the same name but different arguments. Since PHP is a dynamically typed language, this is not possible. Instead PHP supports Default arguments which you can use to get much the same effect.

Poindexter
+2  A: 

If you are dealing with classes you can overload methods with __call() (see Overloading) e.g.:

class Foo {
    public function doSomethingWith2Parameters($a, $b) {

    }

    public function doSomethingWith3Parameters($a, $b, $c) {

    }

    public function __call($method, $arguments) {
      if($method == 'doSomething') {
          if(count($arguments) == 2) {
             return call_user_func_array(($this,'doSomethingWith2Parameters'), $arguments));
          }
          else if(count($arguments) == 3) {
             return call_user_func_array(($this,'doSomethingWith3Parameters'), $arguments));
          }
      }
   }    
}

Then you can do:

$foo = new Foo();
$foo->doSomething(1,2); // calls $foo->doSomethingWith2Parameters(1,2)
$foo->doSomething(1,2,3); // calls $foo->doSomethingWith3Parameters(1,2,3)

This might not be the best example but __call can be very handy sometimes. Basically you can use it to catch method calls on objects where this method does not exist.

But it is not the same or as easy as in Java.

Felix Kling
+1, interesting workaround ;)
Joel Alejandro