Parameters passed by reference
Moderator: General Moderators
Parameters passed by reference
I need a class which can handle any parameters passed to it by reference. Here is an example:
class my_component {
function my_component (&$any_number_of_parameters) {
(for each parameter passed, determine its type (object, array, etc)
and deal with accordingly)
}
}
This will be the base class for a number of other classes:
class another_component expands my_component {
function DoStuff() {
etc
}
}
And then it could be called this way:
$obj = new another_component($param1, $param2, $param3, $param4, etc);
$ojb->DoStuff();
If I can write this class it will be a great help as the passing of variables in the manner described would otherwise have to be coded manually a few hundred times.
How do I get a function to accept any number of parameters, passed by reference?
class my_component {
function my_component (&$any_number_of_parameters) {
(for each parameter passed, determine its type (object, array, etc)
and deal with accordingly)
}
}
This will be the base class for a number of other classes:
class another_component expands my_component {
function DoStuff() {
etc
}
}
And then it could be called this way:
$obj = new another_component($param1, $param2, $param3, $param4, etc);
$ojb->DoStuff();
If I can write this class it will be a great help as the passing of variables in the manner described would otherwise have to be coded manually a few hundred times.
How do I get a function to accept any number of parameters, passed by reference?
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
func_get_args() or func_get_arg() and func_num_args()- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Unfortunately you can't; func_get_args(), func_num_args() and func_get_arg() deliver copies. You could pass the function an array where the elements are references though.
What's the purpose of this?
Code: Select all
[feyd@home]>php -r "$foo1 = 'test'; $foo2 = 'asdf'; $foo3 = 1234; function foo($foo){if (!is_array($foo)) return false; for(reset($foo); current($foo); next($foo)){$foo[key($foo)] = 'foo';}} $arr = array(&$foo1, &$foo2, &$foo3); var_dump($arr); foo($arr); var_dump($arr,$foo1,$foo2,$foo3);"
array(3) {
[0]=>
&string(4) "test"
[1]=>
&string(4) "asdf"
[2]=>
&int(1234)
}
array(3) {
[0]=>
&string(3) "foo"
[1]=>
&string(3) "foo"
[2]=>
&string(3) "foo"
}
string(3) "foo"
string(3) "foo"
string(3) "foo"- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
I'm converting a large pre-existing program to use smarty templates. At the moment the html is produced by hundreds of functions inside the application and its so messy that updating the code outputting the html is a real chore. A lot of the functions have variables and structures in common, so I thought a base class that handles the grunt work would be a real time saver.
I was hoping to have the parameters passed by reference to save memory. Some are large objects or associate arrays. I think passing them all inside an array is the way I might go.
Thanks for your help.
I was hoping to have the parameters passed by reference to save memory. Some are large objects or associate arrays. I think passing them all inside an array is the way I might go.
Thanks for your help.
After some thought it seems that the time taken to build the array before passing it to the function, would be better spent just passing it to a class function: eg:
Code: Select all
$obj = new another_component();
$obj->get_param($param1);
$obj->get_param($param2);
$obj->get_param($param3);
$obj->get_param($param4);
$obj->get_param($param5);
$ojb->DoStuff();
function get_param(&$param) {
if (is_array($param)) {
etc.
}
elseif (is_object($param)) {
etc
} else {
etc
}
}
Last edited by smbeans on Mon Aug 21, 2006 11:56 pm, edited 1 time in total.
feyd | Please use
How do I access the variable name?
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Sorry, I'm stuck (again).
For the function:Code: Select all
$variable = 12;
$obj->get_param($variable);
function get_param(&$param) {
//print variable_name (eg "variable")
print $param; //eg 12
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Yeah I've wanted to do that before too.How do I access the variable name?
Sorry I meant call_user_func_array(). Of course I could test it myself.Feyd wrote:Provided you give it an array where the elements are references as the argument, you're fine.ole wrote:and what about call_user_func()?
That's not really a very good reason to use them. Do you have a design reason why you need to use references? If not you probably shouldn't be using references. I've been programming in PHP for almost 3 years now I've only used references a handful of times, mainly since I started writing object oriented code.I was hoping to have the parameters passed by reference to save memory.