[php-src] Issue #8351: explicit `__construct` call for \DateTimeImmutable object would break immutable
| From: | hormus | Date: | Fri, 15 Apr 2022 17:46:01 +0000 |
| Subject: | [php-src] Issue #8351: explicit `__construct` call for \DateTimeImmutable object would break immutable | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241016@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8351
Comment Author: hormus
The bug exists in the value by reference while with clone by value the constructor does not change
the reference with the exception of the object variable used for the construct.
```php
$tz = new \DateTimeZone('Europe/Rome');
$dateoriginal = new \DateTimeImmutable('1970-01-01 00:00:00.000000', $tz);
$old = $dateoriginal;
$date = (clone $dateoriginal); // DateImmutable::clone from php >= 5.5.0
var_dump($date);
$date->__construct('2000-01-01 00:00:00.000000', $tz);
var_dump($date, $dateoriginal, $old);
```
Correct:
`
object(DateTimeImmutable)#3 (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
object(DateTimeImmutable)#3 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
object(DateTimeImmutable)#2 (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
object(DateTimeImmutable)#2 (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
`
```php
<?php
$tz = new \DateTimeZone('Europe/Rome');
$dateoriginal = new \DateTimeImmutable('1970-01-01 00:00:00.000000', $tz);
$old = $dateoriginal;
$date = $dateoriginal;
var_dump($date);
$date->__construct('2000-01-01 00:00:00.000000', $tz);
var_dump($date, $dateoriginal, $old);
```
incorrect:
`
object(DateTimeImmutable)#2 (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
object(DateTimeImmutable)#2 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
object(DateTimeImmutable)#2 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
object(DateTimeImmutable)#2 (3) {
["date"]=>
string(26) "2000-01-01 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(11) "Europe/Rome"
}
`
Regardless of the read-only property