php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44933 Assignment is done as if by reference
Submitted: 2008-05-07 08:40 UTC Modified: 2008-05-07 12:01 UTC
From: remon at menpmedia dot nl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.6 OS: Fedora Core 4
Private report: No CVE-ID: None
 [2008-05-07 08:40 UTC] remon at menpmedia dot nl
Description:
------------
Problem:
Changing property of a copy of an object results in the changing of the original object as if by reference.

Correct output is from 5.2.4
Incorrect output is from 5.2.6

This problem does NOT occur when using an array!

configure command (taken from phpinfo())
'./configure' '--with-apxs' '--with-curl=/usr/local/lib' '--with-gd' '--enable-gd-native-ttf' '--with-ttf' '--with-gettext' '--with-jpeg-dir=/usr/local/lib' '--with-freetype-dir=/usr/local/lib' '--with-kerberos' '--with-openssl' '--with-mcrypt' '--with-mhash' '--with-mysql=/usr' '--with-mysqli=/usr/bin/mysql_config' '--with-pear' '--with-png-dir=/usr/local/lib' '--with-zlib' '--with-zlib-dir=/usr/local/lib' '--enable-zip' '--with-iconv=/usr/local' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-mbstring'

other info; 
sorry, unavailable; we do not have root access to the server in question. Cannot get a diff between the ini and ini-dist, cannot do a trace.




Reproduce code:
---------------
<?php
$one = new stdClass();
$one->variable = "hello";

$two = $one;
$two->variable = "goodbye";

var_dump($one);
?>


Expected result:
----------------
object(stdClass)#3 (1) {
  ["variable"]=>
  string(5) "hello"
}

Actual result:
--------------
object(stdClass)#1 (1) {
  ["variable"]=>
  string(7) "goodbye"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-07 08:53 UTC] derick@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

Both PHP versions give the same *correct* result that you get under \"Actual result\".
 [2008-05-07 09:19 UTC] remon at menpmedia dot nl
Tested versions:
php 5.2.4 / fedora core 8 / x64: no problem
php 5.2.6 / fedora core 4 / x32: bug occurs
php 5.0.5 / fedora core 4 / x32: bug occurs

php 5.2.4 / win32 : bug occurs (downloaded the binary and executed script)
php 5.2.6 / win32 : bug occurs
php 4.4.8 / win32 : no problem

---
Thoughts: 

Has this behaviour changed in php5? why does ' = ' do the same as ' =& ' ?
Why do 2 different FC4 servers have this problem and an FC8 does not?
Could this be a difference in 32 bit and 64 bit versions?
 [2008-05-07 09:23 UTC] remon at menpmedia dot nl
Bogus ???

$two = $one;

is not the same as

$two =& $one;

PHP4 all platforms understand this
PHP5 on x64 understands this

so, if this is bogus, how do I make a copy of an object and not have changes to the copy affect the original object?

Arrays are unaffected as are normal variables

So, again, if this is bogus, the following is then true;

the assignment operator ' = ' makes a copy of a variable on the righthand side EXCEPT when the righthandside is an object

This does not make sense.
 [2008-05-07 09:23 UTC] scottmac@php.net
The behavior changed between php4 and php5. When you pass around an object it uses the same instance.

The clone keyword will let you duplicate an object.
 [2008-05-07 09:28 UTC] remon at menpmedia dot nl
Thank you scottmac, this helps a bit.

Should I consider PHP5/x64/linux bugged then for NOT reflecting this behavioural change ?
 [2008-05-07 09:31 UTC] derick@php.net
I am running this on 64bit linux, and on 32bit as well. I get the same correct result.
 [2008-05-07 09:36 UTC] remon at menpmedia dot nl
?????
Now I am really confused.

Im running Fedora Core 8 / x64, php 5.2.4, and i have the, by your statement, incorrect result

I understand this has changed and I understand the implication (review all our code which at this point amounts to several million lines of code :( )
I also understand that i need to 'clone' an object. Shame that PHP4 doesn't understand this keyword and throws an error; so much for writing universal code.

Thank you for all your comments and views.
 [2008-05-07 09:39 UTC] remon at menpmedia dot nl
[root@batai ~]# php
<?php
print phpversion();
echo "\n";
$one = new stdClass();
$one->variable = 'hello';

$two = $one;
$two->variable = 'goodbye';

var_dump($one);
?>

5.2.4
object(stdClass)#3 (1) {
  ["variable"]=>
  string(5) "hello"
}

[root@batai ~]#
 [2008-05-07 10:29 UTC] scottmac@php.net
You probably have zend.ze1_compatibility_mode enabled which causes the behavior of php4 but it has bugs and is generally unused.
 [2008-05-07 12:01 UTC] remon at menpmedia dot nl
Wooot.

This did the trick. 

Thanks :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC