#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    60
    Rep Power
    15
    Is it possible to do function overloading in PHP? I can't try it out now so that's why I'm asking. I'm thinking something like this:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <?
    function prnt($text) {
    echo $text;
    }
    function prnt() {
    echo "hello, world";
    }
    [/code]
  2. #2
  3. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    you can't redeclare functions in php, so you can't overload a function in the sense of c++ or java overloading. but since you can call a function with missing parameters, in your test case:

    function print($text)
    {
    if ($text) echo $text;
    else echo "Hello, World!";
    }

    accomplishes pretty much the same thing, yes?
  4. #3
  5. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    No, it doesn't, because if you don't pass the argument, you will get an error.... UNLESS you use a default for the argument:

    function prnt($text="hello, world")
    {
    echo $text;
    }

    If an argument is not passed then the argument will default to "hello, world".

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    60
    Rep Power
    15
    That's exactly what I needed. As far as I knew, you HAD to send it the same amount of arguments, but if rod k's example works, that's what I need. Technically I'd do this for most of what I need:

    function foo($x=0, $y=0, $z=0) { }
    The inputs would never be 0 in my case, so if they're not sent, I can check if ($x) {}

    Thanks!
  8. #5
  9. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    62
    Actually, it looks to me like you CAN do this on PHP4 (at least on 4.0.1pl2, compiled as a module).

    The following code worked fine on my PHP4.0.1pl2 system but generated the missing argument error on my PHP3 system.

    <?
    function prnt($text)
    {
    if ($text) echo $text;
    else echo "Hello, World!";
    }

    prnt();

    ?>

Similar Threads

  1. Uploading Files
    By vivekjain in forum ASP Programming
    Replies: 0
    Last Post: February 17th, 2004, 12:21 AM
  2. database access using thread
    By padmini in forum Visual Basic Programming
    Replies: 3
    Last Post: February 6th, 2004, 01:38 AM
  3. function within function error
    By Germanjulian in forum PHP Development
    Replies: 1
    Last Post: January 7th, 2004, 05:08 AM
  4. Code Benchmarking Tests
    By infamous41md in forum C Programming
    Replies: 4
    Last Post: December 27th, 2003, 05:15 PM
  5. class boolean
    By dbrag in forum C Programming
    Replies: 7
    Last Post: December 11th, 2003, 03:25 PM

IMN logo majestic logo threadwatch logo seochat tools logo