php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33643 Fatal error: Only variables can be passed by reference
Submitted: 2005-07-11 13:06 UTC Modified: 2005-08-16 21:04 UTC
Votes:145
Avg. Score:4.5 ± 0.9
Reproduced:133 of 133 (100.0%)
Same Version:57 (42.9%)
Same OS:57 (42.9%)
From: Jason at hybd dot net Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4CVS-2005-07-18 OS: *
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: Jason at hybd dot net
New email:
PHP Version: OS:

 

 [2005-07-11 13:06 UTC] Jason at hybd dot net
Description:
------------
This bug is a rehash of 33516, but I can't reopen as I'm not the original author.

It seems like you can not pass tempories to function / method parameters.

<?php

class Foo {
	public static function bar() {
		return array('a' => '1234567890');
	}
}

$tmp = Foo::bar();
$var = array_pop($tmp);

?>

Seems to work, but it shouldn't be down to the PHP user (imho) to implement this as this change in PHP 5.1 breaks a lot of existing code.

Reproduce code:
---------------
<?php

class Foo {
	public static function bar() {
		return array('a' => '1234567890');
	}
}

$var = array_pop(Foo::bar());

?>

Expected result:
----------------
$var = '1234567890';
Foo::bar() gets placed in temporary variable

Actual result:
--------------
Fatal error: Only variables can be passed by reference 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-14 11:01 UTC] vrana@php.net
Actual result (4.x after removing "public static"):

4.3.10: No error
4.4.0: No error
5.0.4: No error
5.1.0: Fatal error

Is this really the expected result? I thought 4.4.0 should produce something too. In which cases 4.4.0 produces errors?
 [2005-07-14 11:16 UTC] derick@php.net
I think we need to discuss this a bit more, PHP 4.4 indeed gives no warning or notice on this. Andi, Dmitry?
 [2005-07-14 13:06 UTC] dmitry@php.net
The "Fatal error:" in HEAD and PHP_5_0 occurs because of fix for bug #33257. The patch was not applied into PHP 4.4  because it wasn't affected by this bug (PHP 4 doesn't allow static properties).

Now I found reproducable case for PHP_4_4. :(

<?php
$x = array('foo');

function foo() {
	return $GLOBALS['x'];
}

function bar() {
	static $x = array('bar');
	return $x;
}

echo array_pop(foo())."\n";
echo array_pop(foo())."\n"; /* BUG */
echo array_pop(bar())."\n";
echo array_pop(bar())."\n";
?>
Expected result:
----------------
Fatal error: Only variables can be passed by reference 

Or:
---
foo
foo
bar
bar

Actual result:
--------------
foo

bar
bar


The current behavoir of HEAD is proper from my point of view. PHP_4_4 may be fixed but it will break BC again.

Derick, Andi?

 [2005-07-14 16:37 UTC] rasmus@php.net
This sort of thing needs to throw an error in any version of PHP I think:

  function foo() { return "blah"; }
  function bar(&$arg) { $arg = 1; }
  bar(foo());

While this should work:

  function & foo() { static $a; return $a; }
  function bar(&$arg) { $arg = 1; }
  bar(foo());

And it does in PHP5.1 at least.  The first case indicates a clear bug in the code that we really should be catching.  I suppose we could compromise and throw a warning in PHP4 and then fall back to passing the argument by value, but that is changing the actual workings of the script and is likely going to be wrong.  I think this needs to be part of the reference fixes in PHP4.4.  Sucks that it wasn't caught before the 4.4.0 release, but that's the way it goes.
 [2005-08-16 21:04 UTC] derick@php.net
This is not fixable in PHP 4.4 without breaking BC, so we can't fix it now. Luckily it does not create any memory corruption.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 13:01:29 2024 UTC