ID:               32841
 Updated by:       [EMAIL PROTECTED]
 Reported By:      wagner at bonn dot edu
-Status:           Open
+Status:           Assigned
 Bug Type:         Scripting Engine problem
 Operating System: *
 PHP Version:      4.3.11, 5.0.4
-Assigned To:      
+Assigned To:      andi
 New Comment:

Short version: 
this is expected and the error message clearly says why:
only variables can be referenced and you cannot make a reference to an
expression result.
Long version:
In your case f_default() returns *copy*, not a reference as you wrongly
consider, while f_ref() returns reference and should work indeed.
But I'm a bit surprised to see that f_copy() works, as it should give
the same warning as with f_default().
Concerning the case 2, AFAIK it's because of some reference magic: when
you return $this->v as reference, it turns in refernce itself
(refcount++) and the following call to f_default() returns this
reference.
Though, it would be interesting to see what engine gurus say about it.


Previous Comments:
------------------------------------------------------------------------

[2005-04-26 14:54:41] wagner at bonn dot edu

Description:
------------
When returning a member-variable directly as a reference   
parameter into a function, a fatal error occurs:   
Fatal error: Only variables can be passed by reference ...  
The reproduce code displays the bug (for $case=0) as well 
as two simple workarounds that IMHO show that the 
ZEND-Engine should be able to handle this with no need for 
any error. 
This seems to have been around for a while, at least PHP 
4.3.x  
Probably related to 
http://bugs.php.net/bug.php?id=32840 

Reproduce code:
---------------
$case = 0;

function incr(&$int) {
  return $int++;
}

class foo {
  var $v = 1;
  function &f_ref() { return $this->v; }
  function f_copy() { return (int)$this->v; } //forced copy through
type-cast
  function f_default() { return $this->v; }
}

$foo =& new foo();
switch($case) {
 case 0:
  //default behaviour is broken
  echo incr($foo->f_default())."\n";
 case 1:
   //forced reference works AND makes default behaviour work
afterwards
   echo incr($foo->f_ref())."\n";
   echo incr($foo->f_default())."\n";
   break;
 case 2:
   //forced copy works
   echo incr($foo->f_copy())."\n";
   break;
}

Expected result:
----------------
incr() should get a copy of $foo->v 

Actual result:
--------------
Fatal error: Only variables can be passed by reference in 
foo.php on line 19 
 


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=32841&edit=1

Reply via email to