#36475 [Csd->Bgs]: Call by reference fails for objects
| From: | mike@php.net | Date: | Tue, 21 Feb 2006 22:21:30 +0000 |
| Subject: | #36475 [Csd->Bgs]: Call by reference fails for objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-93499@lists.php.net to get a copy of this message | ||
ID: 36475
Updated by: mike@php.net
Reported By: elmar dot hinz at team-red dot net
-Status: Closed
+Status: Bogus
Bug Type: Class/Object related
Operating System: Linux/Debian
PHP Version: 5.1.2
New Comment:
Not a bug -> Bogus.
Previous Comments:
------------------------------------------------------------------------
[2006-02-21 18:10:57] elmar dot hinz at team-red dot net
It is not the behaviour what I would expect, but indeed no bug. So what
happens in detail? Short analysis of the 2 steps:
1.) On function call $ref get's an alias to the outside variable
$callback.
2.) Before any alterations on this double named variable happen, $ref
is made an alias to a second variable $object (and gives up the fist
variable in the same moment).
Result: $callback and $object are not aliased to each other this way.
------------------------------------------------------------------------
[2006-02-21 13:50:32] mike@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Think again about your code.
------------------------------------------------------------------------
[2006-02-21 13:12:14] elmar dot hinz at team-red dot net
Description:
------------
Cannot access original objects by call by reference.
function tryCallback(&$ref){
$ref =& $this->object;
}
Only a copies work:
function tryCallback(&$ref){
$ref = $this->object;
}
Reproduce code:
---------------
<?php
class myObject{ var $value = 'The original value'; }
function tryCallback(&$ref){
global $object;
$ref =& $object; // in a class it would be: $ref =& $this->object;
dump($ref, 'Before the proplem: The object is still here but will
fail from outside.');
}
function dump($var, $name){
print '<p style="background:#eeeeee;">' . $name .
'</p>';
if($var){ print '<pre style="color:green">'; print_r($var); print
'</pre>'; }
else{ print '<p style="color:red"><strong>$var is
empty!!!</strong></p>'; }
}
$object = new myObject();
tryCallback($callback);
dump($callback, 'Here the object is missing in my test.');
$callback->value = 'Try to set a value.';
dump($callback, 'Ooops, what a funny object now.');
dump($object, 'This is the original object. Unaltered. No reference was
created.');
?>
Expected result:
----------------
Dump 1:
myObject Object
(
[value] => The original value
)
Dump 2:
myObject Object
(
[value] => The original value
)
[...]
Actual result:
--------------
Dump 1:
myObject Object
(
[value] => The original value
)
Dump 2:
$var is empty!!!
[...]
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=36475&edit=1