3

I'm having some weird problems with a DateTime() class.

Everytime I try this:

$object = new DateTime();
var_dump($object->date);

The var_dump() returns me NULL, but if I do this:

$object = new DateTime();
var_dump($object);
var_dump($object->date);

both var_dump() gives me the correct answer and I have no idea why. Have anyone seem this before?

Caio Favero
  • 2,196
  • 3
  • 17
  • 18

1 Answers1

7

It has something to do with PHP being forced to create a full object representation when you dump the whole object which happens to update the object state, as opposed to you just accessing the one individual property.

The bigger point is though that you don't really have any business accessing DateTime::$date at all. It's an undocumented internal implementation detail that you're not supposed to use anywhere. If you want to output the date, use DateTime::format or one of the other public interface methods.

deceze
  • 510,633
  • 85
  • 743
  • 889