|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[1998-12-07 16:06 UTC] zeev
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 11:00:01 2025 UTC |
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... :(