Page 1 of 1

overloaded functions

Posted: Fri Sep 23, 2005 12:56 pm
by thegreatone2176
Is there any plans to implement overloaded functions in php? In C++ it is very useful to be able to overload functions and have the differnet functions call based on how many pararments there is. for example in c++

int show()
{
cout << "hi";
}

int show(int a)
{
cout << a;
}

int main()
{
show();
show(1);
return 0;
}

It is a very useful thing to have avaiable and I am wondering why php has never implemented it. Is there some reason it cant be done or was there some former decision saying it wont be included?

Posted: Fri Sep 23, 2005 1:19 pm
by feyd
although I'm not privey to the "internal" talks, I'd guess not very likely.. although I'd like to get it.. I know several people who would not..

Posted: Fri Sep 23, 2005 1:23 pm
by Charles256
if there's a petition for it add my name to the list :-D Potentially very useful. Fire function referring to fire on a torch and firing a bow is the example I got. :-D

Posted: Fri Sep 23, 2005 1:55 pm
by Weirdan
well, it's easy to emulate using either func_get_args() or __call:

Code: Select all

// using your example
function show() {
  if(!func_num_args()) {
     echo "hi";
  } else {
     echo func_get_arg(0);
  }
}

show();
show(1);

Posted: Fri Sep 23, 2005 1:57 pm
by feyd
true, it can be emulated somewhat easily.. just would feel a bit weird having to write an interface to a function :P

Posted: Fri Sep 23, 2005 2:14 pm
by Jenk
Is this similar to polymorphism? In that the functions can detect which type of call or object you are referring, it can then respond accordingly? Or have I got the wrong end of it?

Posted: Fri Sep 23, 2005 2:18 pm
by Weirdan
feyd wrote:just would feel a bit weird having to write an interface to a function :P
well, after writing a handful of functions that generate functions in javascript this approach doesn't seem to be weird :)

Posted: Fri Sep 23, 2005 3:08 pm
by feyd
Weirdan wrote:well, after writing a handful of functions that generate functions in javascript this approach doesn't seem to be weird :)
touche my friend. After oddities of real polymorphism yes, I'd have to agree.

Re: overloaded functions

Posted: Fri Sep 23, 2005 7:18 pm
by Christopher
thegreatone2176 wrote:Is there any plans to implement overloaded functions in php? In C++ it is very useful to be able to overload functions and have the differnet functions call based on how many pararments there is. for example in c++

int show()
{
cout << "hi";
}

int show(int a)
{
cout << a;
}

int main()
{
show();
show(1);
return 0;
}

It is a very useful thing to have avaiable and I am wondering why php has never implemented it. Is there some reason it cant be done or was there some former decision saying it wont be included?
You can do the same thing in PHP like the example given or just:

Code: Select all

function show($a='') {
    if ($a == '') {
        echo 'hi';
    } else {
        echo $a;
    }
}

show();
show(1);
If you wanted multiple types you could do something like:

Code: Select all

function show() {
  if(func_num_args()) {
    $a =  func_get_arg(0);
    if (is_array($a)) {
      foreach ($a as $val) {
        echo "$val<br/>";
      }
    } else {
      echo $a;
    }
  } else {
      echo "hi";
  }
}

show();
show(1);
show(array('one','two','three'));
Different language, different way to do things.

Posted: Fri Sep 23, 2005 11:15 pm
by thegreatone2176
thanks for the replies I never knew about func_num_args() which does handle the situation quite well