Jump to content

Detect arguments in function


Go to solution Solved by .josh,

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

Edited by ankur0101
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;
Edited by Ch0cu3r

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 by ankur0101

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?

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

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 */
  }
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.