Parameters passed by reference

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
smbeans
Forum Newbie
Posts: 8
Joined: Sun Mar 12, 2006 7:13 pm

Parameters passed by reference

Post by smbeans »

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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

func_get_args() or func_get_arg() and func_num_args()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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.

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"
What's the purpose of this?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You could have just said, "use an array".
and what about call_user_func()?
smbeans
Forum Newbie
Posts: 8
Joined: Sun Mar 12, 2006 7:13 pm

Post by smbeans »

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ole wrote:You could have just said, "use an array".
I felt an illustration was in order.
ole wrote:and what about call_user_func()?
Provided you give it an array where the elements are references as the argument, you're fine.
smbeans
Forum Newbie
Posts: 8
Joined: Sun Mar 12, 2006 7:13 pm

Post by smbeans »

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.
smbeans
Forum Newbie
Posts: 8
Joined: Sun Mar 12, 2006 7:13 pm

Post by smbeans »

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
}
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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can't.. well you can, technically, but it's annoying and error prone.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

How do I access the variable name?
Yeah I've wanted to do that before too.
Feyd wrote:
ole wrote:and what about call_user_func()?
Provided you give it an array where the elements are references as the argument, you're fine.
Sorry I meant call_user_func_array(). Of course I could test it myself.
I was hoping to have the parameters passed by reference to save memory.
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.
Post Reply