[PHP-DEV] Bug #962: Inconsistent Object behavior: when referenced from array v. referenced directly From: dwood <email protected>
Date: 11/30/98

From: dwood <email protected>
Operating system: Win95
PHP version: 3.0.5
PHP Bug Type: Other
Bug description: Inconsistent Object behavior: when referenced from array v. referenced directly

Here we go:

<?
//////////////////////////////////////////////////////////////////////////////////

class A
{
        var $entries;
}

class B
{
        var $message;
}

$newA = new A;

// Now we pack the A object into an array.
$external_array[0] = $newA;

$newB = new B;
$newB->message = "testing...";

// We put it into A's array:
$newA->entries[] = $newB;

// Now we get it back directly from the A object:
$temp1 = $newA->entries[0]; // Temp1 should be our B object
print("You'll see the string: ".$temp1->message."<P>");

// And then we'll try getting back via the array:
$damaged_by_external_array = $external_array[0]; // This will be our A object (or what's left of it)

// This is where entries should be empty...
$temp2 = $damaged_by_external_array->entries[0]; // Temp2 should have been our retreived B object.
print("You won't see a thing: ".$temp2->message."<P>"); // Instead, this line returns an error that temp2 is, in fact, not an object.

print("<HR>");
//////////////////////////////////////////////////////////////////////////////////

?>

I've noticed that if I add the A object to the array _after_ the B object has been
added to $newA->entries, it works fine.

I'm hoping that when I add something to an array it's not _copied_ into the array -
just referenced there. That would make this a bug. However, I bet everything I put
into an array (including potentially _huge_ objects) probably _are_ copied, aren't they?
Eeesh. Well, if I'm right about that, then is it at least _possible_ to add references
instead of values to an array? I've noticed

$a[0] = &$myobj;

generates a parse error.

Let me just say: languages that usually pass by value are great, but only languages
that usually pass by reference are any good for object oriented coding. That's fine
if the former is more important than the latter... but it'll make this OO junkie
a bit sad... :(

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>