Jump to content

Number of arguements in a function


Cep

Recommended Posts

Hello,

I was just wondering if anyone here could tell me if there is a limit on the number of arguements that can be passed to a user defined function or if there are any disadvantages in having large numbers of arguments?

Thanks

Link to comment
Share on other sites

Not that I'm aware of. You can always pass in array references or use func_num_args. If you have [i]too[/i] many arguments, you should ask yourself what you're really try to accomplish because this is a design issue. You may need to break your function into multiple--each being a modular piece of the puzzle. You may also prefer the OOP route.
Link to comment
Share on other sites

[quote author=Cep link=topic=107356.msg430606#msg430606 date=1157726992]if there is a limit on the number of arguements that can be passed to a user defined function[/quote]
I don't think so respectively I think if there is a limit it would be too high to reach. ;)

[quote author=Cep link=topic=107356.msg430606#msg430606 date=1157726992]if there are any disadvantages in having large numbers of arguments?[/quote]
Im my opinion you should not put too many arguments into a function, but there is no fix "rule" or something like that. I think that it becomes kind of chaotic, if you have eight or twelve parameters. If I'm honest, I cannot imagine why one should want to do this .. ;)

Regards, Ben.
Link to comment
Share on other sites

The disadvantage to having large numbers of arguments is that you'll get them the wrong way round sometimes, or find it difficult to see which values you're passing as which argument.

If your function needs a large number of arguments consider creating a class instead and making it a member function of a class from which you can instantiate an object with the arguments as member variables.
Link to comment
Share on other sites

Hehe thanks,

The reason for so many arguements is because I am creating several INSERT and UPDATE queries which I am going to be reusing all over the place, rather then write the code each time I thought I would place it in a function and then call that function when I need it.
Link to comment
Share on other sites

He might be using it for use in a query builder.. such as this method:
[code]<?php

public function parseQuery()
{
    $num = func_num_args();
    if ($num == 1)
    {
        $this->_query = func_get_arg(0);
    }
    elseif ($num > 1)
    {
        $query = func_get_arg(0);
        for ($i = 1; $i < $num; $i++)
        {
            $arg = $arg = mysql_real_escape_string(func_get_arg($i), $this->_link);
            if (!is_numeric($arg))
            {
                $arg = "'" . mysql_real_escape_string($arg, $this->_link) . "'";
            }
            $query = str_replace(':' . $i, $arg, $query);
        }
        $this->_query = $query;
    }
    else
    {
        throw new InvalidArgumentException('Must provide a query to be parsed!');
    }
}

?>[/code]

usage..

[code]<?php

$db->parseQuery('SELECT * FROM `table` WHERE `col` = :1 AND `col2` = :2', $colVal, $col2Val);

?>[/code]
Link to comment
Share on other sites

Or if I'm not using Mysql :D but I didn't specify a database so that doesnt matter. Thankyou all for your suggestions, I think I may adapt Jenks idea or use an array depending on which is easier to implement at this stage.

Thanks!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.