php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #962 Inconsistent Object behavior: when referenced from array v. referenced directly
Submitted: 1998-11-30 19:16 UTC Modified: 1998-12-07 16:06 UTC
From: dwood at templar dot com Assigned:
Status: Closed Package: Other
PHP Version: 3.0.5 OS: Win95
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dwood at templar dot com
New email:
PHP Version: OS:

 

 [1998-11-30 19:16 UTC] dwood at templar dot com
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...  :(

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1998-12-07 16:06 UTC] zeev
Ok, as my 'hunch' suggested, this isn't a bug, but a result
of the fact that all copying in PHP 3.0 is done by value.

There may be reference support in the future, but it's not
something we can implement right now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 08:01:33 2024 UTC