Page 1 of 1

Question about parameters

Posted: Tue Dec 21, 2010 10:33 pm
by thedanny09
Hey guys.

so heres the scoop.

I have a variable $var = "first, second, third"; and im trying to pass that to a function foo that takes three arguments. IS there a way to do $this->foo($var) and the function see's $var as passing three parameters? I want to avoid passing an array.

Example:

function foo($first, $second, $third) { ... }

$var = "1, 2, 3";

foo($var);

I get an error when i do this, saying it sees it as only passing 1 param, which makes sense since its a string. How would I go about doing this?

Re: Question about parameters

Posted: Tue Dec 21, 2010 10:43 pm
by califdon
Why do you want to avoid using an array? That would seem to be the most straightforward approach. If you are certain that there will always be 3 and only 3 parameters, you could pass it as a string and use explode() to create 3 variables inside your function, but that is a pain in you-know-where if some day there are only 2 values, or you need 4 values, and anyway it requires more code.

Oh wait, I just re-read your question -- you said the function requires 3 arguments. If that's the case, then it requires 3 arguments. One string is not 3 arguments.

Re: Question about parameters

Posted: Wed Dec 22, 2010 3:02 am
by dejan
thedanny09 wrote:I have a variable $var = "first, second, third"; and im trying to pass that to a function foo that takes three arguments. IS there a way to do $this->foo($var) and the function see's $var as passing three parameters? I want to avoid passing an array.
What you need is: http://php.net/manual/en/function.call- ... -array.php

You would do something like:

Code: Select all

function foo($first, $second, $third) { ... }
$var = "1, 2, 3";
call_user_func_array("foo", explode(", ", $var));
 
call_user_func_array's first parameter is the name of the function you want to call (foo), and it's second parameter is an array of parameters to be passed to foo. So, you'd use http://www.php.net/manual/en/function.explode.php to convert the string to an array.

Re: Question about parameters

Posted: Wed Dec 22, 2010 6:09 am
by Jsphlane20
I'm really interested in knowing why you would not like to pass an array to this function. The answer to that question will help everyone understand what you are trying to accomplish.

Re: Question about parameters

Posted: Wed Dec 22, 2010 12:45 pm
by thedanny09
califdon wrote:Why do you want to avoid using an array? That would seem to be the most straightforward approach. If you are certain that there will always be 3 and only 3 parameters, you could pass it as a string and use explode() to create 3 variables inside your function, but that is a pain in you-know-where if some day there are only 2 values, or you need 4 values, and anyway it requires more code.

Oh wait, I just re-read your question -- you said the function requires 3 arguments. If that's the case, then it requires 3 arguments. One string is not 3 arguments.
The issue is this is dynamic; so the 3 arguments is a possibility, but it can call any function that i specify and provide arguments for. So i can say, make an object of type X, use the function abc, and pass it these parameters. Because its dynamic, i feel that using a "string of parameters" if this is possible, is the best way to program it in the long run, and it is much easier to read and diagnose. Also, i dont need to worry about array errors, and dont forget this needs to be multi-demsional if thats the case, and then its the time it takes to search through the array and get the information. Eventually with hundreds of different functions doing hundreds of lines of code, i can see this possibly being a problem. which is why i wanted to do simple parameter passing.

Re: Question about parameters

Posted: Wed Dec 22, 2010 12:47 pm
by thedanny09
dejan wrote:
thedanny09 wrote:I have a variable $var = "first, second, third"; and im trying to pass that to a function foo that takes three arguments. IS there a way to do $this->foo($var) and the function see's $var as passing three parameters? I want to avoid passing an array.
What you need is: http://php.net/manual/en/function.call- ... -array.php

You would do something like:

Code: Select all

function foo($first, $second, $third) { ... }
$var = "1, 2, 3";
call_user_func_array("foo", explode(", ", $var));
 
call_user_func_array's first parameter is the name of the function you want to call (foo), and it's second parameter is an array of parameters to be passed to foo. So, you'd use http://www.php.net/manual/en/function.explode.php to convert the string to an array.
Hey;

Im going to try this out now, ill let you know how it went. Thanks

Re: Question about parameters

Posted: Wed Dec 22, 2010 12:57 pm
by thedanny09
dejan wrote:
thedanny09 wrote:I have a variable $var = "first, second, third"; and im trying to pass that to a function foo that takes three arguments. IS there a way to do $this->foo($var) and the function see's $var as passing three parameters? I want to avoid passing an array.
What you need is: http://php.net/manual/en/function.call- ... -array.php

You would do something like:

Code: Select all

function foo($first, $second, $third) { ... }
$var = "1, 2, 3";
call_user_func_array("foo", explode(", ", $var));
 
call_user_func_array's first parameter is the name of the function you want to call (foo), and it's second parameter is an array of parameters to be passed to foo. So, you'd use http://www.php.net/manual/en/function.explode.php to convert the string to an array.

One last question about this. My function is inside a class outside of this one, meaning I need to create an object of that type before calling that function.

So it would be something like $foo = new foo(); $foo->functionName..... but morphed into the call_user_func_array.....

is it the same type of implementation

Re: Question about parameters

Posted: Thu Dec 23, 2010 3:59 am
by dejan
thedanny09 wrote: One last question about this. My function is inside a class outside of this one, meaning I need to create an object of that type before calling that function.

So it would be something like $foo = new foo(); $foo->functionName..... but morphed into the call_user_func_array.....

is it the same type of implementation
call_user_func_array accepts any callback as the first parameter. So, to call method "functionName" of an object "foo", you'd write it as:

Code: Select all

$foo = new FooClass();
call_user_func_array(array('foo', 'functionName'), explode(whatever));
So, if you pass an array as the first parameter instead of a string, the first member of the array (foo) is the name of the object, and the second member of the array is the name of the method (functionName). You can read more about it here: http://www.php.net/manual/en/language.p ... s.callback