[SOLVED] Strange Reference Behavior

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
ahaig
Forum Newbie
Posts: 6
Joined: Tue Mar 01, 2005 9:42 am

[SOLVED] Strange Reference Behavior

Post by ahaig »

This seems to me like a very simple and self-contained loop, so I don't really understand how this is happening.

I run the block of code and out comes an array with two copies of the same object:

Code: Select all

foreach ( $dbInfo as $this_index => $this_db_data )	{
     $objsї] = new $obj_type( $this_db_data, true );
}
$dbInfo is just an array of keys => database values, and the constructor loads these values into variables and sets "is_new" to true (since it's a new record).

$obj_type comes from above and is just a name of the class. The objects do seem to end up being the proper class.

When I do a similar block:

Code: Select all

foreach ( $dbInfo as $this_index => $this_db_data )	{
     $new_obj = new $obj_type( $this_db_data, true );
     print_r($new_obj);
     $objsї] = $new_obj;
}
Then I can tell that the objects are being loaded properly originally, but somehow the second object is copying over the first.

This doesn't make any sense to me.
Any insight?

Thanks
-Asher
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you are using php4.. objects are passed via copy by default, not reference.
ahaig
Forum Newbie
Posts: 6
Joined: Tue Mar 01, 2005 9:42 am

Post by ahaig »

No - I'm using php5.

And even if that was true, the point is that they aren't being passed by copy - somehow both _new_ objects are becoming the same object when placed in an array.
ahaig
Forum Newbie
Posts: 6
Joined: Tue Mar 01, 2005 9:42 am

Post by ahaig »

Same result if I do this:

Code: Select all

$new_obj1 = new $obj_type( $dbInfoї0], true );
	$new_obj2 = new $obj_type( $dbInfoї1], true );

	print_r($new_obj1);
	print_r($new_obj2);
	die('blah');
I end up with two copies of the same object.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe your $dbInfo array is wrong? Because the following works:

Code: Select all

<?php

class test
&#123;
	function test($var1, $var2)
	&#123;
		$this->var1 = $var1;
		$this->var2 = $var2;
	&#125;
&#125;

$obj_type = 'test';

$new_obj1 = new $obj_type( 'foo', true );
$new_obj2 = new $obj_type( 'bar', true );

print_r($new_obj1);
print_r($new_obj2);

?>
outputs

Code: Select all

test Object
(
    &#1111;var1] => foo
    &#1111;var2] => 1
)
test Object
(
    &#1111;var1] => bar
    &#1111;var2] => 1
)
ahaig
Forum Newbie
Posts: 6
Joined: Tue Mar 01, 2005 9:42 am

Post by ahaig »

No - definitely not wrong. I can see it when I print_r/var_dump it, _and_ when I load the object it is correct - once I load a second object the first becomes the same as the second.

I think the answer lies somewhere in the way references are working but I don't even really know where to start looking - how could two calls to new obj() possibly have anythign to do with each other?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that all depends on your code, I would suspect.
ahaig
Forum Newbie
Posts: 6
Joined: Tue Mar 01, 2005 9:42 am

Post by ahaig »

Yes I would think so.

Any suggestions what sort of things to look for? I don't understand how a reference in one object could get attached to a reference in a separate object of the same class.
ahaig
Forum Newbie
Posts: 6
Joined: Tue Mar 01, 2005 9:42 am

Post by ahaig »

So I went to do some more debugging and tried loading the objects into $GLOBALS rather than local copies. All of a sudden the code works properly. So I remove the code I just added and go back to the original code I posted. All of a sudden it is working properly. I didn't change anything.

I am, to say the least, a bit confused. Something inconsistent is going on here, because I haven't changed any code.
Post Reply