Question about parameters
Moderator: General Moderators
-
thedanny09
- Forum Newbie
- Posts: 5
- Joined: Fri Nov 26, 2010 8:38 am
Question about parameters
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?
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
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.
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.
Last edited by califdon on Tue Dec 21, 2010 10:44 pm, edited 1 time in total.
Reason: Re-read the question
Reason: Re-read the question
Re: Question about parameters
What you need is: http://php.net/manual/en/function.call- ... -array.phpthedanny09 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.
You would do something like:
Code: Select all
function foo($first, $second, $third) { ... }
$var = "1, 2, 3";
call_user_func_array("foo", explode(", ", $var));
-
Jsphlane20
- Forum Newbie
- Posts: 17
- Joined: Wed Aug 11, 2010 1:17 pm
- Contact:
Re: Question about parameters
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.
-
thedanny09
- Forum Newbie
- Posts: 5
- Joined: Fri Nov 26, 2010 8:38 am
Re: Question about parameters
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.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.
-
thedanny09
- Forum Newbie
- Posts: 5
- Joined: Fri Nov 26, 2010 8:38 am
Re: Question about parameters
Hey;dejan wrote:What you need is: http://php.net/manual/en/function.call- ... -array.phpthedanny09 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.
You would do something like:
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.Code: Select all
function foo($first, $second, $third) { ... } $var = "1, 2, 3"; call_user_func_array("foo", explode(", ", $var));
Im going to try this out now, ill let you know how it went. Thanks
-
thedanny09
- Forum Newbie
- Posts: 5
- Joined: Fri Nov 26, 2010 8:38 am
Re: Question about parameters
dejan wrote:What you need is: http://php.net/manual/en/function.call- ... -array.phpthedanny09 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.
You would do something like:
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.Code: Select all
function foo($first, $second, $third) { ... } $var = "1, 2, 3"; call_user_func_array("foo", explode(", ", $var));
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
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: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
Code: Select all
$foo = new FooClass();
call_user_func_array(array('foo', 'functionName'), explode(whatever));