Pass 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Pass by reference

Post by vigge89 »

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:

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
	//#################################
}
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.
Post Reply