Page 1 of 1

how to write a func_num_args_needed()?

Posted: Tue May 24, 2005 6:53 pm
by Ollie Saunders
woo! there's nothing more exciting than a first post on a forum. well maybe there is but i'm geeky enough to think its exciting. anywho, enough blurb.

I've got a function, it just so happens its a constructor but that's besides the point. Its got 4 arguments and I want to die if the an incorrect number of arguments are supplied to it. At the moment I'm doing something like this:

Code: Select all

define('FOO_REQUIRED_ARGS',4);
function foo($arg1,$arg2,$arg3,$arg4) {
   $num_args  = func_num_args(); 
   if ($num_args != FOO_REQUIRED_ARGS) die('bad call');
}
but this has its drawbacks. if i want to change the number of arguments, which i no doubt will, then i have to change the constant, which i'll no doubt forget to do. what i'm really looking for is a function called func_num_args_needed(); which would return the number of args required by the calling function.

any php geniuses out there with a solution to this very very small problem? or is this just a language immaturity issue.

oLE

Posted: Tue May 24, 2005 7:05 pm
by AISBERG
you can equal the arguments with $i and then $num_args<=$i can solve your problem with number changing

Posted: Tue May 24, 2005 7:20 pm
by Ollie Saunders
what if i want to add a new arg not just take one away?

Posted: Tue May 24, 2005 8:09 pm
by harrisonad
don't specify the function arguments

Code: Select all

function foo(){
it will be fine like what the manual said
http://www.science.uva.nl/ict/ossdocs/p ... -args.html