overloaded functions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

overloaded functions

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

true, it can be emulated somewhat easily.. just would feel a bit weird having to write an interface to a function :P
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: overloaded functions

Post 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.
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

thanks for the replies I never knew about func_num_args() which does handle the situation quite well
Post Reply