php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30394 Assignment operators yield wrong result with __get/__set
Submitted: 2004-10-11 11:24 UTC Modified: 2005-06-03 17:39 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at hartwerk dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-STABLE-03-23 OS: Linux
Private report: No CVE-ID:
 [2004-10-11 11:24 UTC] php at hartwerk dot com
Description:
------------
When there is a function on the right-hand side of an assignment operator expression, and the variable is to be accessed via __get/__set, the operation yields wrong results. 

Reproduce code:
---------------
class Container
{
	public function __get( $what )
	{
		return $this->_p[ $what ];
	}
	
	public function __set( $what, $value )
	{
		$this->_p[ $what ] = $value;
	}
	
	private $_p = array();
}

$c = new Container();
$c->a = 1;
$c->a += 1;
print $c->a;	// --> 2

print " - ";
$c->a += max( 0, 1 );
print $c->a;	// --> 4 (!)

Expected result:
----------------
2 - 3

Actual result:
--------------
2 - 4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-15 12:13 UTC] ante dot dfg at moj dot net
This code works if you return the value from _get via reference.... try:

public function &__get( $what ) {
    return $this->_p[ $what ];
}
 [2004-10-15 18:46 UTC] php at hartwerk dot com
An easier work-around would be $c->a = $c->a + max( 0, 1 ), but work-arounds do not solve bugs..
 [2004-11-04 16:10 UTC] tony2001@php.net
It's 5.0.x specific bug.
 [2005-03-23 22:51 UTC] tony2001@php.net
With clean 5.0.x-CVS I get:
2
Fatal error: Unsupported operand types in index.php on line 23 
with the code below.
HEAD works just fine.
 [2005-06-03 17:39 UTC] dmitry@php.net
Fixed in PHP_5_0.
This bug was already fixed in HEAD long time ago with new garbage collector.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 00:01:21 2014 UTC