Edit report at https://bugs.php.net/bug.php?id=48476&edit=1
ID: 48476
Comment by: jasper at nerdsweide dot nl
Reported by: rvanvelzen at expert-shops dot com
Summary: cloning extended DateTime class without calling
parent::__constr crashed PHP
Status: Assigned
Type: Bug
Package: Reproducible crash
Operating System: CentOS 5.2
PHP Version: 5.2.9
Assigned To: derick
Block user comment: N
Private report: N
New Comment:
This bug still exists in PHP 5.3.6:
-------------------------
class ExtendedDateTime extends DateTime
{}
$date1 = new ExtendedDateTime();
$date2 = clone $date1; // <= ok!
-------------------------
class ExtendedDateTime extends DateTime
{
public function __construct( $time = 'now', DateTimeZone $timezone = null )
{
parent::__construct( $time, $timezone );
}
}
$date1 = new ExtendedDateTime();
$date2 = clone $date1; // <= ok!
-------------------------
class ExtendedDateTime extends DateTime
{
public function __construct( $time = 'now', DateTimeZone $timezone = null )
{
}
}
$date1 = new ExtendedDateTime();
$date2 = clone $date1; // <= php crashes
-------------------------
In the apache2 error_log I find:
[notice] child pid 63203 exit signal Bus error (10)
Another note, when I implement a __clone method, it is called and run without
problems:
-------------------------
class ExtendedDateTime extends DateTime
{
public function __construct( $time = 'now', DateTimeZone $timezone = null )
{
}
public function __clone()
{
var_dump( 'Am I run?' );
exit;
}
}
Outputs:
object(ExtendedDateTime)[345]
string 'Am I run?' (length=9)
-------------------------
What's even more unexpected is that the constructor has some effect on the
language construct 'clone'. As far as I know the constructor isn't used when
cloning an object.
The case I'm using is an extend of DateTime which wraps around an original
DateTime. I've worded around this bug by implementing a 'copy' method:
-------------------------
class ExtendedDateTime extends DateTime
{
protected $_datetime;
public function __construct( $time = 'now', DateTimeZone $timezone = null )
{
$this->_datetime = new DateTime( $time, $timezone );
}
public function copy()
{
$copy = unserialize( 'O:9:"F500_Date":0:{}' );
$copy->_datetime = clone $this->_datetime;
return $copy;
}
}
This works fine, but I'd like to be able to use the 'clone' language construct
;)
System: Mac OS X 10.5.8 (9L31a)
Kernel: Darwin 9.8.0
Apache: Apache/2.2.19 (Unix)
PHP: 5.3.6 (Zend Engine v2.3.0 with Xdebug v2.1.1)
Previous Comments:
------------------------------------------------------------------------
[2009-06-05 09:29:05] rvanvelzen at expert-shops dot com
Description:
------------
When trying to clone an extended class of DateTime of which the constructor
doesn't call parent::__construct, this causes PHP to die.
Reproduce code:
---------------
<?php
class MyDateTime extends DateTime {
public function __construct() { }
}
var_dump(clone new MyDateTime);
?>
Expected result:
----------------
object(MyDateTime)#2 (0) {
}
Actual result:
--------------
Nothing, PHP crashes
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=48476&edit=1