|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49188 DateTime::__construct should allow a DateTime-object to be passed as argument
Submitted: 2009-08-07 08:18 UTC Modified: 2017-01-11 17:17 UTC
Avg. Score:3.6 ± 1.7
Reproduced:5 of 6 (83.3%)
Same Version:2 (40.0%)
Same OS:2 (40.0%)
From: niklas at narhinen dot net Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.10 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: niklas at narhinen dot net
New email:
PHP Version: OS:


 [2009-08-07 08:18 UTC] niklas at narhinen dot net
DateTime::__construct should allow a DateTime object to be passed as parameter to emulate good object desing (Copy constructor).

This wouldn't break anything.

Reproduce code:

class MyClass
	private $myDateTime;
	public function setTime($datetime = null) { $this->myDateTime = new DateTime($datetime); }
	public function getTime($format = "Y-m-d") { return $this->myDateTime->format($format); }

$class = new MyClass();
$dt = new DateTime('15.5.2005');

Expected result:
string(10) "2009-08-07"
string(10) "2009-08-07"
string(10) "2005-05-15"

Actual result:
string(10) "2009-08-07"
string(10) "2009-08-07"
PHP Fatal error:  Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, object gi
ven' in D:\temp\php\datetime.php:7
Stack trace:
#0 D:\temp\php\datetime.php(7): DateTime->__construct(Object(DateTime))
#1 D:\temp\php\datetime.php(17): MyClass->setTime(Object(DateTime))
#2 {main}
  thrown in D:\temp\php\datetime.php on line 7


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 15:15 UTC]
-Package: Feature/Change Request +Package: Date/time related
 [2012-12-04 20:16 UTC] will dot sskates at ntlworld dot com
You can pass the time string instead. What is the use case for this?
 [2016-06-20 16:12 UTC]
> DateTime::__construct should allow a DateTime object to be
> passed as parameter to emulate good object desing (Copy
> constructor).

PHP has `clone` for this purpose. In my opinion, that's pretty
 [2017-01-11 17:17 UTC]
-Status: Open +Status: Not a bug
 [2017-01-11 17:17 UTC]
As this issue is by now over 7 years old, targets an unsupported version of PHP and can be circumvented in several ways I'm closing this.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 19 05:01:30 2024 UTC