Page 1 of 1
[SOLVED] Strange Reference Behavior
Posted: Tue Mar 01, 2005 9:50 am
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
Posted: Tue Mar 01, 2005 10:23 am
by feyd
you are using php4.. objects are passed via copy by default, not reference.
Posted: Tue Mar 01, 2005 11:26 am
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.
Posted: Tue Mar 01, 2005 12:07 pm
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.
Posted: Tue Mar 01, 2005 12:14 pm
by feyd
maybe your $dbInfo array is wrong? Because the following works:
Code: Select all
<?php
class test
{
function test($var1, $var2)
{
$this->var1 = $var1;
$this->var2 = $var2;
}
}
$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
(
їvar1] => foo
їvar2] => 1
)
test Object
(
їvar1] => bar
їvar2] => 1
)
Posted: Tue Mar 01, 2005 12:21 pm
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?
Posted: Tue Mar 01, 2005 12:25 pm
by feyd
that all depends on your code, I would suspect.
Posted: Tue Mar 01, 2005 12:28 pm
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.
Posted: Tue Mar 01, 2005 12:39 pm
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.