ankur0101 Posted October 2, 2013 Share Posted October 2, 2013 (edited) Hi, I think subject tells you everything about question. Suppose I have following code > <?php class Car { public function drive($driver='', $car_model='') { echo "Wrooom !!!"; } } $car = new Car; $car->drive('Matt', 'BMW'); ?> SO what I want to do is if developer types $car->drive('Matt', 'BMW', 'randomstring') It should throw error. Means the method/function drive() should contain only 2 attributes, if 3rd attribute value is passed, it should throw error. How to do that ? Thanks Edited October 2, 2013 by ankur0101 Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/ Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 (edited) You can check how many arguments are being sent to a function using the func_num_args function. Example function myFunc($arg1, $arg2) { if(func_num_args() > 2) trigger_error('Can only pass two arguments to ' . __METHOD__ . ' method', E_USER_ERROR); // function code here } myFunc('one', 'two', 'three') // failes; myFunc('one', 'two') // passes; Edited October 2, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/#findComment-1452230 Share on other sites More sharing options...
ankur0101 Posted October 2, 2013 Author Share Posted October 2, 2013 (edited) Hi, Is there any way to find how many arguments are DEFINED in a function ? Means is there any way to show that there are 2 arguments defined in myFunc ? So after writing func_num_args() and comparing it with 2 is there any way to automatically detect how many attributes are defined ? Thanks Edited October 2, 2013 by ankur0101 Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/#findComment-1452246 Share on other sites More sharing options...
.josh Posted October 2, 2013 Share Posted October 2, 2013 if func_num_args() returns 2 then that means there are 2 arguments passed to your function. Additionally you can use func_get_args() to get the argument values passed to your function. I'm not sure what you mean by attributes vs. arguments? Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/#findComment-1452250 Share on other sites More sharing options...
AbraCadaver Posted October 2, 2013 Share Posted October 2, 2013 Only reflection as far as I know: $func = new ReflectionMethod('car', 'drive'); echo $func->getNumberOfParameters()." total\n"; echo $func->getNumberOfRequiredParameters()." required\n"; print_r( $func->getParameters() ); Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/#findComment-1452251 Share on other sites More sharing options...
Solution .josh Posted October 2, 2013 Solution Share Posted October 2, 2013 well func_num_args() and func_get_args() work independently from the default values provided in the parameter definitions. Example: function myFunc($foo='bar') { $args = func_get_args(); if (!isset($args[0])) { echo 'user did not give a first argument, using default for $foo='.$foo; } else { if ($args[0]==$foo) { echo 'user gave a first argument and it is the same value as default. $foo='.$foo.' and $args[0]='.$args[0]; } else { echo 'user gave a first argument and it is not the same value as default. $foo='.$foo.' and $args[0]='.$args[0]; } } } echo myFunc(); // output the 1st echo in the function echo myFunc('bar'); // outputs the 2nd echo in the function echo myFunc('foobar'); // outputs the 3rd echo in the function So to handle the following scenarios: - check if user gave more or less arguments than you expect: use func_num_args - simply use default values if values aren't passed: provide default value in parameter definition - determine if $foo is default value or if user happen to pass same value as default: compare parameter to func_get_args (first param is element 0, etc.). Alternately you can not set a default value for your params in the param list, and put that logic within the function. Advantage of this is that you don't need to compare $foo with func_get_args()[0] : Example: function myFunc($foo) { // user did not pass a first argument if (!isset($foo)) { /* assign a default value to $foo or return an error or whatever you want */ } } Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/#findComment-1452256 Share on other sites More sharing options...
ankur0101 Posted October 3, 2013 Author Share Posted October 3, 2013 Thanks Josh Quote Link to comment https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/#findComment-1452472 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.