php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18842 cloning functions improperly, creates references and not copies
Submitted: 2002-08-10 02:15 UTC Modified: 2002-08-10 13:17 UTC
From: ben-bugs dot php dot net at efros dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS-2002-08-10 OS: linux
Private report: No CVE-ID: None
 [2002-08-10 02:15 UTC] ben-bugs dot php dot net at efros dot com
class tmp {
 var $tmp = '';
 function __construct($t) {
  $this->tmp=$t;
 }
}
$x = new tmp(4);
$y = $x;
echo "x = ".$x->tmp."<BR>\n"; /* prints 4 */
echo "y = ".(++$y->tmp)."<BR>\n"; /* prints 5 */
echo "x = ".$x->tmp."<BR>\n"; /* prints 5, but should be 4 still */

I guess the $y is still referencing the $x class ...

Also, if I create __clone(), it doesn't appear to execute.

This was tested on CVS 2002-8-8 SAPI and also 4.3.0 alpha 2 CLI

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-10 02:21 UTC] ben-bugs dot php dot net at efros dot com
I guess that if I explicitly call __clone(), everything goes okay.  I guess the problem is that it thinks I mean make reference when using the "equals" assignment ....  ?
 [2002-08-10 07:23 UTC] wez@php.net
in ZE2:

$x = $y; // when is_object($y), make $x a reference to $y
$x = $y->__clone(); // make a copy of $y

It should say that in the Zend/ZEND_CHANGES file.
 [2002-08-10 11:58 UTC] ben-bugs dot php dot net at efros dot com
Shouldn't "=" be a "copy / assign" operation and "$var1=&$var2" be a reference the original operation?

This change will break backward compatibility with existing php applications, but more importantly it will confuse developers between the differences of object copies and variable copies.

IMHO, I think this issue is being handled wrong.  __clone() should be called by default when assigning via "="
 [2002-08-10 13:17 UTC] wez@php.net
This is not a discussion forum: use the zend engine 2 mailing list for that: engine2@lists.zend.com.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 18:00:01 2026 UTC