php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36475 Call by reference fails for objects
Submitted: 2006-02-21 13:12 UTC Modified: 2006-02-21 23:21 UTC
From: elmar dot hinz at team-red dot net Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.1.2 OS: Linux/Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: elmar dot hinz at team-red dot net
New email:
PHP Version: OS:

 

 [2006-02-21 13:12 UTC] 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!!!

[...]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-21 13:50 UTC] 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 18:10 UTC] 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 23:21 UTC] mike@php.net
Not a bug -> Bogus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 07:01:26 2025 UTC