Edit report at https://bugs.php.net/bug.php?id=62532&edit=1

 ID:                 62532
 User updated by:    matthew1471 at matthew1471 dot co dot uk
 Reported by:        matthew1471 at matthew1471 dot co dot uk
 Summary:            Changes To A Copied DateTime Variable Always Changes
                     Original
 Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Windows Server 2003 SP2
 PHP Version:        5.3.14
 Block user comment: N
 Private report:     N

 New Comment:

Thanks, I thought my array code meant it was not by design... but having 
thought about it perhaps arrays are treated the same as primitive types.

Thanks for confirming it's not a bug. I hope this gets archived for others to 
find on their searches :)

I used "clone" in the example for those that are looking to see an example of 
its usage, just didn't think that was the recommended way of manipulating the 2 
variables.

Matthew


Previous Comments:
------------------------------------------------------------------------
[2012-07-11 19:19:56] ni...@php.net

Heh, sorry for that default message at the top of my comment. Clicked on the 
wrong thing :/

------------------------------------------------------------------------
[2012-07-11 19:19:08] ni...@php.net

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

$foo = $bar does not copy the object in PHP. It just creates a new variable 
pointing to the same object.

If you want to really copy the object use
$foo = clone $bar;

------------------------------------------------------------------------
[2012-07-11 14:20:19] matthew1471 at matthew1471 dot co dot uk

Description:
------------
Interacting with a copied DateTime variable (without using the reference '&' 
syntax) still changes the original variable.

Test script:
---------------
<?php
// Get today's date.
$originalDate = date_create();;

// Take a copy.
$copiedDate = $originalDate;
// This *will* work for some reason : $copiedDate = clone $originalDate;

echo 'originalDate=' . date_format($originalDate, 'Y-m-d') . '<br/>';

// Add 1 day.
date_add($copiedDate, date_interval_create_from_date_string('1 days'));
// This doesn't work either : 
$copiedDate->add(date_interval_create_from_date_string('1 days'));

// $originalDate *SHOULD* still be the same.
echo 'originalDate=' . date_format($originalDate, 'Y-m-d') . '<br/>';

// This behaviour if desired is different with other objects:
$originalString = array('Sausages');
echo $originalString[0] . '<br/>';
$copiedString = $originalString;
$copiedString[0] = 'Ice Cream';
echo $originalString[0] . '<br/>';
?>

Expected result:
----------------
The $originalDate to remain the same as it was before $copiedDate was changed.

Actual result:
--------------
$originalDate changes.


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



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

Reply via email to