Req #51280 [Bgs]: Calculate expression before using it as argument

From: Date: Mon, 22 Mar 2010 09:18:41 +0000
Subject: Req #51280 [Bgs]: Calculate expression before using it as argument
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-148832@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51280&edit=1

 ID:          51280
 Updated by:  johannes@php.net
 Reported by: olamedia at gmail dot com
 Summary:     Calculate expression before using it as argument
 Status:      Bogus
 Type:        Feature/Change Request
 Package:     *General Issues
 PHP Version: 5.3.2

 New Comment:

The order of the evaluation of function parameters is "undefined". (It
actually changed with 5.2 or so due to performance reasons)


Previous Comments:
------------------------------------------------------------------------
[2010-03-22 09:51:47] olamedia at gmail dot com

Sorry, my bad. This was an incomplete example. In first message there
was a good 
example, when $a->x is an object, and in both cases: a($a->x) and
a($a->x = 3) - I 
'm expecting object $a->x instead of assigned value because of __set()
function, 
which converts value to an object.

------------------------------------------------------------------------
[2010-03-22 09:43:51] olamedia at gmail dot com

> the value of "$a = 3" is 3

Here I'm talking about function arguments, not a simple assignments.

function b($a){
  // here expected resulting object $a, not just int(3)
  var_dump($a);
}
class a{}
$a = new a();
b($a->x = 3); // outputs int(3)
$a->x = 3
b($a); // outputs object(a)#2 (1) { ["x"]=> int(3) }

------------------------------------------------------------------------
[2010-03-21 21:07:08] hholzgra@php.net

Expected behavior, see:

http://php.net/manual/en/language.operators.assignment.php

  [...]
  The value of an assignment expression is the value assigned. 
  That is, the value of "$a = 3" is 3.  
  [...]

------------------------------------------------------------------------
[2010-03-12 08:45:19] olamedia at gmail dot com

Description:
------------
Calculate expression before using it as argument in function and with
echo.

Test script:
---------------
class p{
	protected $_name = null;
	protected $_value = null;
	public function __construct($name, $value){
		$this->_name = $name;
		$this->_value = $value;
	}
	public function is($value){
		$this->_value = $value;
		return $this;
	}
	public function __toString(){
		return "$this->_name = $this->_value";
	}
}
class a{
	protected $_a = null;
	public function __set($name, $value){
		$this->_a[$name] = new p($name, $value);
		return $this->_a[$name];
	}
	public function __get($name){
		if (!isset($this->_a[$name])){
			$this->_a[$name] = new p($name, null);
		}
		return $this->_a[$name];
	}
}
class q{
	public function where($var){
		echo $var;
	}
}
$a = new a();
echo $a->x = 2; // "2" (Expected: "x = 2")
echo "\r\n";
echo $a->x; // "x = 2"
echo "\r\n";
$q = new q();
$q->where($a->x = 2); // "2" (Expected: "x = 2")
echo "\r\n";
$q->where(($a->x = 2)); // "2" (Expected: "x = 2")

Expected result:
----------------
x = 2
x = 2
x = 2
x = 2

Actual result:
--------------
2
x = 2
2
2


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



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


Thread (7 messages)

« previous php.bugs (#148832) next »