Jump to content

Detect arguments in function


ankur0101

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/282645-detect-arguments-in-function/
Share on other sites

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;

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

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() );

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 */
  }
}

Archived

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

×
×
  • Create New...

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.