Question about parameters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thedanny09
Forum Newbie
Posts: 5
Joined: Fri Nov 26, 2010 8:38 am

Question about parameters

Post 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?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Question about parameters

Post 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.
Last edited by califdon on Tue Dec 21, 2010 10:44 pm, edited 1 time in total.
Reason: Re-read the question
dejan
Forum Newbie
Posts: 8
Joined: Tue Dec 21, 2010 6:11 am

Re: Question about parameters

Post 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.
Jsphlane20
Forum Newbie
Posts: 17
Joined: Wed Aug 11, 2010 1:17 pm
Contact:

Re: Question about parameters

Post 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.
thedanny09
Forum Newbie
Posts: 5
Joined: Fri Nov 26, 2010 8:38 am

Re: Question about parameters

Post 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.
thedanny09
Forum Newbie
Posts: 5
Joined: Fri Nov 26, 2010 8:38 am

Re: Question about parameters

Post 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
thedanny09
Forum Newbie
Posts: 5
Joined: Fri Nov 26, 2010 8:38 am

Re: Question about parameters

Post 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
dejan
Forum Newbie
Posts: 8
Joined: Tue Dec 21, 2010 6:11 am

Re: Question about parameters

Post 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
Post Reply