php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49382 can't access DateTime->date
Submitted: 2009-08-27 07:52 UTC Modified: 2010-11-18 12:29 UTC
Votes:36
Avg. Score:4.0 ± 0.9
Reproduced:34 of 35 (97.1%)
Same Version:23 (67.6%)
Same OS:10 (29.4%)
From: klawd at kamundo dot de Assigned: derick
Status: Assigned Package: Date/time related
PHP Version: 5.3.0 OS: Debian GNU/Linux 5.0
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-08-27 07:52 UTC] klawd at kamundo dot de
Description:
------------
Can not access property date of DateTime.

Reproduce code:
---------------
$dt=new DateTime('1742-05-23 00:00:00'); echo $dt->date;
gets me Notice: Undefined property: DateTime::$date

strangely though, this works:
$dt=new DateTime('1742-05-23 00:00:00'); print_r($dt); echo $dt->date;
DateTime Object ( [date] => 1742-05-23 00:00:00 [timezone_type] => 3 [timezone] => UTC ) 1742-05-23 00:00:00



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-07 20:22 UTC] derick@php.net
-Package: Date/time related +Package: Feature/Change Request
 [2010-03-07 20:22 UTC] derick@php.net
->date being available is actually a side-effect of support for var_dump() here. I'll mark this as a feature request as it was not intended to work.
 [2010-08-04 22:36 UTC] weirdan at gmail dot com
if this was not intended to work this way, why won't you just fix it so that no 
properties are created as a result of print_r / Reflection::export() / foreach() / 
property_exist / array cast?
 [2010-11-18 12:29 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
 [2012-12-29 17:56 UTC] info at metashock dot net
I think it is interesting that the property can be accessed using reflection. Using the following example I can access the property:

$dt = new DateTime();
$o = new ReflectionObject($dt);
$p = $o->getProperty('date');
$date = $p->getValue($dt)); // returns the value of DateTime::$date
 [2013-07-09 11:29 UTC] hanskrentel at yahoo dot de
I wonder how this qualifies as a bug as any object can have variable properties 
and accessing undefined ones give notices.

Just sounds like standard PHP behavior here.
 [2013-07-09 11:35 UTC] weirdan at gmail dot com
I'd say the bug is not that the property is not present before var_dump'ing 
object, but that the property is suddenly created. Unexpected side-effect.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Mon Apr 21 04:01:57 2014 UTC