Development php function arguments

Discussion in 'Software' started by RTT, 18 Jun 2006.

  1. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    PHP:
    function foo()
    {
        return 
    "bar";
    }

    echo 
    foo('randomjunk');

    echo 
    foo('randomjunk'1212, array('foo''bar'));

    Given the above function foo() which does not take any arguments, why does PHP not throw a fit when you specify arguments to the function when making a call to it?

    I've wasted a day thanks to that - tracking a bug down. I was calling a function that was buried deep in another script that I was providing an argument for, forgetting that i hadn't built in functionality relating to the argument yet. Half of it was my fault of course, but if php had said "hey you've given an argument where one isn't specified" then it'd have been 2 minutes to fix and not 8 hours!

    silly php. php4 by the way if it makes a difference.

    Anyone have any clues as to why this wouldn't throw an error/warning, even with E_ALL set as the error reporting level? Maybe i'm being dumb but I can't think of a good reason why not. php4 has no overloading...
     
  2. Nedsbeds

    Nedsbeds Badger, Slime, Weasel!!

    Joined:
    16 May 2002
    Posts:
    1,972
    Likes Received:
    9
    Well, it doesn't throw an error in php 5 either.
    I just put it down to the fact that oo still isn't very good in php.
    And if it did produce an error message, chances are it wouldn't be very useful ;)
     
  3. Ramble

    Ramble Ginger Nut

    Joined:
    5 Dec 2005
    Posts:
    5,596
    Likes Received:
    42
    It's probabily jsut a notice, ignore it, all it's telling you is that you're a lousy coder. :)
     
  4. dfhaii

    dfhaii internets

    Joined:
    24 Mar 2002
    Posts:
    520
    Likes Received:
    0
    It's not a notice though, as it doesn't say *anything*. PHP complains when you miss an argument off, but continues to run the function anyway, yet doesn't say when you have too many arguments. Seems like a bug to me.
     
  5. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    This isn't only limited to OO, and has nothing to do with OO anyway - it's a syntax thing.

    Not sure if your comment about an error message was a bash at php or not, but i'm sure the error message would be as useful as the one is when you miss out a required argument... :)

    Woah, way to not even read the thread there buddy.

    As dfhaii said, it doesn't even throw a notice or warning... let alone an error. It carries on as if nothing is wrong at all - try it!
     
  6. Ramble

    Ramble Ginger Nut

    Joined:
    5 Dec 2005
    Posts:
    5,596
    Likes Received:
    42
    Whoops, sorry about that, I just skimmed the post and assumed it was asking for help not asking a question.
     
  7. Hwulex

    Hwulex What's a Dremel?

    Joined:
    1 Feb 2002
    Posts:
    4,007
    Likes Received:
    1
    Whilst in this case, I'm sure it's an annoyance, it can actually be incredibily useful.

    Whilst you're right, this is a syntax thing, it does apply to both procedural and OO practices.

    Imagine, if you like, a method that sets bitwise flags for an object. The beauty with being able to pass more than the specified number of parameters is that you don't need the method or function to know how many you've sent, or have to do, in this example, the bitwise calculations outside the method.

    The beauty is, php has the built in function func_get_args() which returns an array of the arguments (or parameters) sent to the function from which that is called.

    It's probably easier explained in code:

    PHP:
    public static function setFlag() {
        
    $arrArgs func_get_args();
        foreach( 
    $arrArgs as $intValue ) {
            
    self::$intFlags self::$intFlags $intValue;
        }
    }
    With me? Excellent.


    That's just one example I can think of immediately because we actually use it at work, but it applies to any method to which you might want to send any number of arguments. Pretty much the same thing, but how about a function to add together a bunch of numbers?

    PHP:
    function sum() {
        
    $intSum 0;

        foreach ( 
    func_get_args() as $intArg ) {
            
    $intSum += $intArg;
        }

        return 
    $intSum;
    }
    Called with:
    PHP:
    echo sum144321234123634 );
    // prints 1240
    Of course, that's a pointless exercise because you could just call array_sum, but that'd mess up my example. :p
     
    Last edited: 18 Jun 2006
  8. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Ahhhh :D I couldn't believe for a minute that it'd be a bug that had somehow survived this long.

    Good to know, thanks :)
     

Share This Page