#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    9
    Rep Power
    0
    Lo all,
    I have try overloading function/constructor with numbers of arguments but it doesnt work. Anybody knows why? Thanx in advace!

    for instance,
    function foo($arg_1) {
    ...
    }

    function foo($$arg_1, $arg_2) {
    ...
    }

    php keeps calling "function foo($$arg_1, $arg_2)" even when i only pass one argument in. any idea?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    16
    I'm pretty sure PHP isn't capable of function overloading ala C++ but there's a workaround that may (or may not) work for you depending on your needs

    set $arg2 to some default value using the = operator in your function declaration

    function foo($arg1, $arg2="")
    {
    ....
    }

    that way you can call
    foo($arg1);

    or

    foo($arg1, $arg2);

    I suppose you could run a test on $arg2 to see if it exists and do something with it - probably not what you're looking for but that's the best I've got
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    London
    Posts
    46
    Rep Power
    15
    My sentiments exactly.

Similar Threads

  1. overloading constructors
    By iso in forum PHP Development
    Replies: 5
    Last Post: May 26th, 2009, 11:00 PM
  2. Replies: 3
    Last Post: January 31st, 2004, 01:16 AM
  3. Operator overloading: anyone with experience?
    By sherzodr in forum Perl Programming
    Replies: 0
    Last Post: April 13th, 2003, 03:32 AM
  4. Debugging operator overloading
    By Tozilla in forum C Programming
    Replies: 5
    Last Post: March 30th, 2003, 04:37 PM
  5. overloading of functions/operators
    By Bjoern in forum PHP Development
    Replies: 3
    Last Post: November 9th, 2001, 08:12 AM

IMN logo majestic logo threadwatch logo seochat tools logo