Page 1 of 1

[Solved] Dynamic Argument Generation

Posted: Mon Jan 10, 2005 8:33 am
by thomas777neo
Author: thomas777neo (stpxc01.sentechsa.net)
Date: 10/01/2005 09:20

Hi All

I need to dynamically generate some variables that need to be sent to a function, I have an example of what I am trying to do. The problem is, the stuff I am sending to the function is an array of variables (e.g. array($var1,$var2)). I actually really need to send them as the actual variables.

I tried to use the get_defined_vars() and the extract functions, but to no avail.

Here is the code (sample not run through parser, merely for conceptual purpose):

Code: Select all

<?php
$insert_table = "my_table"; 

$fields = $ADO-> getParameters($insert_table); // Get Fields for Table 

// Extra Fields passed as parameters 
$parameters = array("\$insert_table","\$option","\$duplicate_detect","\$select_table","\$insert_fields","\$select_fields"); 

foreach ($fields as $get_field) 
{ 
$preString = "insert_"; 

$field = $get_field->name; 

if ($field <> "session" and $field <> "entity_id") // Exclude Defaults Fields from concatination 
{ 
$field = "\$".$preString.$field; 
} //if ($field <> "session" and $field <> "entity_id") 
else 
{ 
$field = "\$".$field; 
} // else for if ($field <> "session" and $field <> "entity_id") 

array_push($parameters,$field); // Push the newly generated field onto the array stack 

} //foreach ($fields as $get_field) 

// HERE IS WHERE IS NEED THE HELP OF SOME HOW CONVERTING THE ARRAY INTO ACTUAL VARIABLES 
$get_data = $my_class-> get_my_data($parameters); // send the variables through 
?>

Posted: Mon Jan 10, 2005 7:41 pm
by rehfeld
list()

Example

Posted: Tue Jan 11, 2005 1:26 am
by thomas777neo
Please show me how to use it in a practical example, remember that I only want to send on parameter to the function.

Posted: Tue Jan 11, 2005 1:50 am
by rehfeld
have you read the manual?

http://www.php.net/list


in case thats not what you wanted, also look at func_get_args(),
look at the manual for examples.

OK

Posted: Tue Jan 11, 2005 5:41 am
by thomas777neo
Yes, I have looked at the manual.

I am trying to understand how to use it. For example:

In the first example I showed you, the $parameters variable looks something like this : $insert_table,$another_variable,$third_variable.

I tried this:

Code: Select all

$check = (list($whatever)=$parameters);
$check outputs $insert_table. What do I say in the brackets where it says $whatever? So that $check would actually be $insert_table,$another_variable,$third_variable. Remember though, I don't want to have multiple variables where $whatever is at the moment.

I just need to fully understand what's cooking.

Re: OK

Posted: Tue Jan 11, 2005 8:57 am
by feyd
thomas777neo wrote:What do I say in the brackets where it says $whatever? So that $check would actually be $insert_table,$another_variable,$third_variable.
that's just a copy of the variable then. If you need to combine together a few of the values stored in the array, you need to pull them out individually:

Code: Select all

$check = array();
$check&#1111;] = $parameters&#1111;0];
$check&#1111;] = $parameters&#1111;2];
$check&#1111;] = $parameters&#1111;5];
if you want the values to keep their index:

Code: Select all

$check&#1111;2] = $parameter&#1111;2];

OK

Posted: Tue Jan 11, 2005 9:28 am
by thomas777neo
Ok, I tried this:

Code: Select all

$increment = sizeof($parameters);
		$check = array();
		
		$i = 0;
		while ($increment > $i)
		&#123;
			$check&#1111;] = $parameters&#1111;$i];
			$i++;
		&#125; //while ($increment > $i)
		
		//var_dump($check);

		$insertEntity = $entity-> insert_entity($check&#1111;0]);
I understand that $check[0] is $insert_table.

For the function to work properly, I would need to do something like this:

Code: Select all

$insertEntity = $entity-> insert_entity($check&#1111;0],$check&#1111;1]...);
What I am actually aiming for is to do something like this:

Code: Select all

$insertEntity = $entity-> insert_entity($check&#1111;]);
So, $check[] should represent the $insert_table,$option that I would have actually written like this:

Code: Select all

$insertEntity = $entity-> insert_entity($insert_table,$option);
If I'm not understanding it, I apologise. Arrays are not my strong point. If I'm not getting it, please show me a simple example of what I should do. Thank You.

Posted: Tue Jan 11, 2005 9:35 am
by feyd
your code just copies the contents of $parameters.

As for your wish to call the function, you can literally just pass the array:

Code: Select all

$return = $your_class->your_function($parameters)
Knowing that all the parameters are passed as an array is up to the function now.

Posted: Wed Jan 12, 2005 2:31 am
by thomas777neo
I know that I can pass the Array to the function and then break it up in my class. I am trying not to send an array, I am trying to sort of convert the array into the actual arguments that I want to send to the function.

Instead of

Code: Select all

$whatever = $my_class-> my_function($parameters);
I want to convert $parameters so that it doesn't send the array. But actually represents $insert_table,$option.

So, when I send $parameters it mustn't be an array or string. It must actually be one argument that I send to the function that it is the same as $insert_table,$option... not being the Array but being the actual arguments that I want to send without breaking it up on the other side.

I know it doesn't sound sane, I just want to know if it is possible.

Posted: Wed Jan 12, 2005 2:42 am
by feyd
no, it's not.