how to write a func_num_args_needed()?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

how to write a func_num_args_needed()?

Post 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
AISBERG
Forum Newbie
Posts: 8
Joined: Sat May 21, 2005 3:17 pm
Location: Bucharest
Contact:

Post by AISBERG »

you can equal the arguments with $i and then $num_args<=$i can solve your problem with number changing
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

what if i want to add a new arg not just take one away?
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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
Post Reply