From:             niklas at narhinen dot net
Operating system: 
PHP version:      5.2.10
PHP Bug Type:     Feature/Change Request
Bug description:  DateTime::__construct should allow a DateTime-object to be 
passed as argument

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

-- 
Edit bug report at http://bugs.php.net/?id=49188&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49188&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49188&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49188&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49188&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49188&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49188&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49188&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49188&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49188&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49188&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49188&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49188&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49188&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49188&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49188&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49188&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49188&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49188&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49188&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49188&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49188&r=mysqlcfg

Reply via email to