Strange behaviour with references

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
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Strange behaviour with references

Post by mme000 »

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]


Hi all. I've a problem with variable references which is really driving me crazy.
First of all, I'm running my code on a Linux server with Apache/2.0 and PHP 4.4.4.

Now, the code.

Code: Select all

<?php

$refs = array();

class MyTest1
{
	var $value = 0;
	
	function MyTest1()
	{
		global $refs;
		
		$refs[] = &$this;

		$this->value = 1234;
	}
}

class MyTest2
{
	var $value = 0;
	
	function MyTest2()
	{
		global $refs;
		
		$refs[] = &$this;		
	}
	
	function SetValue()
	{
		$this->value = 1234;
	}
}

$inst1 = new MyTest1;
$inst2 = new MyTest2;

$inst2->SetValue();

print_r($refs);

?>
As you can see we have two classes (MyTest1 and MyTest2) which, in their constructor, insert a reference to themselves in an array ($refs[] = &$this);
Both the classes have a data member ($value) whose value is initialized to 0 and will be changed during execution.
The only difference between the two classes is that MyTest1 sets the new value ($this->value = 1234) in the constructor while MyTest2 sets the new value in another member function (SetValue()) which will be called from outside the class after instantiation ($inst2->SetValue());
AFAIK the two operations should produce the same result, but, if you try to execute this code, you will receive this result:

Array ( [0] => mytest1 Object ( [value] => 1234 ) [1] => mytest2 Object ( [value] => 0 ) )

As you can see, the value of the data member for MyTest2 reference is still 0! I really don't understand what I'm doing wrong.
Any help will be greatly appreciated.


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 »

Your "new" calls create copies of the objects. You need to add the reference operator to both.
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Post by mme000 »

Hi feyd,
I'm sorry for the mistake in posting code. I didn't read that post... :oops:
And thank you very much for the answer.
Changing from

Code: Select all

$inst1 = new MyTest1;
$inst2 = new MyTest2;
to

Code: Select all

$inst1 =& new MyTest1;
$inst2 =& new MyTest2;
solves my problem.
Post Reply