Pass by reference
Posted: Mon Aug 01, 2005 7:21 am
I'm trying to create a reference in a class which points to a variable in another class, but I'm not sure where i should put ampersands in the code. What I want to do is to have the first class initiate another class with an argument which refers to a variable in the first class. The second class should then store the reference in a var and be able to read and modify the variable from the first class.
I read the php.net documentation, but I still can't figure out how to make it work this way.
My current code:
Thanks in advance, I hope you all understood 
Edit: changed to [ code ] as [ php ] wasn't working.
Edit2: Wouldn't work either, back to [ php ] then.
I read the php.net documentation, but I still can't figure out how to make it work this way.
My current code:
Code: Select all
class page {
var $vars = array ();
//#################################
//## { output - output page
function output () {
// initiate parser with a reference to $vars
$parser = new parser (&$vars);
} ## } output
//#################################
}
class parser {
var $data = array ();
//#################################
//## { construct
function parser (&$data_origin) {
// $data should refer to $vars in the page class
$data = &$data_origin;
// return success
return true;
} ## } construct
//#################################
}Edit: changed to [ code ] as [ php ] wasn't working.
Edit2: Wouldn't work either, back to [ php ] then.