php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62532 Changes To A Copied DateTime Variable Always Changes Original
Submitted: 2012-07-11 14:20 UTC Modified: 2012-07-11 20:12 UTC
From: matthew1471 at matthew1471 dot co dot uk Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.14 OS: Windows Server 2003 SP2
Private report: No CVE-ID: None
 [2012-07-11 14:20 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-11 19:19 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-07-11 19:19 UTC] nikic@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 19:19 UTC] nikic@php.net
Heh, sorry for that default message at the top of my comment. Clicked on the wrong thing :/
 [2012-07-11 20:12 UTC] matthew1471 at matthew1471 dot co dot uk
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
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun May 04 05:01:28 2025 UTC