Jump to content

Archived

This topic is now archived and is closed to further replies.

Cep

Number of arguements in a function

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
indeed, but that could involve reworking 90+% of his site, and a headache full of troubles when some hosts don't use mysqli :)

Share this post


Link to post
Share on other sites
Depending on what you want to do in the end, you may use an associative array as argument.
Though Jenk's suggestion is also a nice one .. ;)

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

×

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.