0

I want to create a variable argument function, which will insert a new row into a table. The function will contain table name,attribute,its value.Since no of attributes in a table is variable, variable number of arguments should be provided.Can you suggest an idea?

Prinz Km
  • 323
  • 1
  • 12
  • 1
    Not much to go off here, get the arguments in the function using `$args = func_get_args();`? Or use an array for the `attribute => value` mappings? – naththedeveloper Sep 19 '13 at 11:40
  • Whilst not strictly a duplicate, I suspect you'd benefit from a read of [Should my PHP functions accept an array of arguments or should I explicitly request arguments](http://stackoverflow.com/questions/2112913/should-my-php-functions-accept-an-array-of-arguments-or-should-i-explicitly-requ/2112949#2112949), as it discusses the main approaches. – John Parker Sep 19 '13 at 11:41
  • You could pass in an object with properties' values representing the variables you want to pass in. Then inside the function, iterate over the properties of the object. – IsisCode Sep 19 '13 at 11:41
  • An associative array is clearer, faster, more intuitive to people foreign to your code, and it allows not to provide certain values contrarly to arguments where you can't not pass arg N if you pass args > N. Besides if you need to pass extra parameters to your function that are not attributes or values, it will be hard to tell/remember which ones are attribute/values and which ones are not. – Virus721 Sep 19 '13 at 11:46

2 Answers2

1

func_get_args is probably what you are looking for.

http://php.net/manual/en/function.func-get-args.php

aspyct
  • 3,625
  • 7
  • 36
  • 61
0

func_num_args in combination with func_get_args will return the amount of arguments supplied to a function and their values. Although from what it sounds like, you should accept your attributes and values using arrays.

Mark
  • 1,376
  • 9
  • 16