Cep Posted September 8, 2006 Share Posted September 8, 2006 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted September 8, 2006 Share Posted September 8, 2006 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. Quote Link to comment Share on other sites More sharing options...
°°Ben³ Posted September 8, 2006 Share Posted September 8, 2006 [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. Quote Link to comment Share on other sites More sharing options...
king arthur Posted September 8, 2006 Share Posted September 8, 2006 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 8, 2006 Share Posted September 8, 2006 the answer to how many arguments a function can have, the universe, and everything, is 42. Quote Link to comment Share on other sites More sharing options...
Cep Posted September 8, 2006 Author Share Posted September 8, 2006 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 8, 2006 Share Posted September 8, 2006 what about building the query string and then passing just the one string to the function? Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 He might be using it for use in a query builder.. such as this method:[code]<?phppublic 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] Quote Link to comment Share on other sites More sharing options...
effigy Posted September 8, 2006 Share Posted September 8, 2006 Also see mysqli_prepare. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 indeed, but that could involve reworking 90+% of his site, and a headache full of troubles when some hosts don't use mysqli :) Quote Link to comment Share on other sites More sharing options...
°°Ben³ Posted September 8, 2006 Share Posted September 8, 2006 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 .. ;) Quote Link to comment Share on other sites More sharing options...
Cep Posted September 11, 2006 Author Share Posted September 11, 2006 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.