|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-11-24 15:15 UTC] jani@php.net
-Package: Feature/Change Request
+Package: Date/time related
[2012-12-04 20:16 UTC] will dot sskates at ntlworld dot com
[2016-06-20 16:12 UTC] cmb@php.net
[2017-01-11 17:17 UTC] heiglandreas@php.net
-Status: Open
+Status: Not a bug
[2017-01-11 17:17 UTC] heiglandreas@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 11:00:01 2025 UTC |
Description: ------------ 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: --------------- <?php date_default_timezone_set('Europe/Helsinki'); 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(); $class->setTime("7.8.2009"); var_dump($class->getTime()); $class->setTime(null); var_dump($class->getTime()); $dt = new DateTime('15.5.2005'); $class->setTime($dt); var_dump($class->getTime()); ?> 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