Bug #75232 [Dup->ReO]: print_r creating side-effect.
| From: | jhdxr@php.net | Date: | Fri, 22 Sep 2017 17:24:00 +0000 |
| Subject: | Bug #75232 [Dup->ReO]: print_r creating side-effect. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-211324@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75232&edit=1
ID: 75232
Updated by: jhdxr@php.net
Reported by: ben at redsnapper dot net
Summary: print_r creating side-effect.
-Status: Duplicate
+Status: Re-Opened
Type: Bug
Package: Date/time related
Operating System: any
PHP Version: 7.1.9
Block user comment: N
Private report: N
New Comment:
@daverandom, bug #49382 was marked as "not a bug" because you believe that's a
feature request rather than a bug. I don't think it's a good reason to close issue, but
that't fine.
but this issue focus on the different behaviors of
$d->date before and after
print_r. There is no doubt that this inconsistency is a bug.
Previous Comments:
------------------------------------------------------------------------
[2017-09-22 11:41:46] daverandom@php.net
Dupe of https://bugs.php.net/bug.php?id=49382
------------------------------------------------------------------------
[2017-09-20 09:05:04] gooh@php.net
For the sake of completeness, this also happens when var_dump'ing or
ReflectionObject::export'ing:
php > $dt = new DateTime;
php > var_dump($dt);
object(DateTime)#3 (3) {
["date"]=>
string(26) "2017-09-20 10:57:54.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
php > echo $dt->date;
2017-09-20 10:57:54.000000
php > $dt = new DateTime;
php > ReflectionObject::export($dt);
Object of class [ <internal:date> class DateTime implements DateTimeInterface ] {
// some lines omitted â¦
- Dynamic properties [3] {
Property [ <dynamic> public $date ]
Property [ <dynamic> public $timezone_type ]
Property [ <dynamic> public $timezone ]
}
// some lines omitted â¦
}
php > echo $dt->date;
2017-09-20 10:57:57.000000
So it adds three public properties.
------------------------------------------------------------------------
[2017-09-20 08:38:41] ben at redsnapper dot net
Description:
------------
Using print_r on a DateTime object creates a side-effect: the property 'date' is now set
and publicly available.
Test script:
---------------
//Expected Behaviour
php > $d1 = DateTime::createFromFormat("Ymd\THis\Z", '20170920T091600Z');
php > echo $d1->date;
Notice: Undefined property: DateTime::$date
//Unexpected Behaviour / side effect.
php > $d2 = DateTime::createFromFormat("Ymd\THis\Z", '20170920T091600Z');
php > print_r($d2->date);
DateTime Object
(
[date] => 2017-09-20 09:16:00.000000
[timezone_type] => 3
[timezone] => UTC
)
php > echo $d2->date;
2017-09-20 09:16:00.000000
Expected result:
----------------
There should be no change (side-effect) made to an object when using print_r()
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75232&edit=1