1

I simply cannot find an explanation for this bizarre behavior:

Given the following array:

array (size=2)
  0 => 
    object(Craft\DateTime)[225]
      public 'date' => string '2017-02-01 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)
  1 => 
    object(Craft\DateTime)[231]
      public 'date' => string '2017-02-02 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)

If I do the following:

foreach($myArray as $data){
     var_dump($data->date);
}

The result of the var_dump is NULL. Not only is that incorrect but if before the foreach I add the following:

var_dump($test);

I then get output. So how could a var_dump possibly have any bearing on the value of itself. It just doesn't make any sense.

yivi
  • 42,438
  • 18
  • 116
  • 138
Alan A
  • 2,557
  • 6
  • 32
  • 54
  • 3
    `var_dump` actually does add the `date` property. It does seems strange when you first encounter it. – Don't Panic Feb 01 '17 at 19:27
  • I encountered something similar when I asked this question: http://stackoverflow.com/questions/38487774/what-are-these-undocumented-properties-in-dateinterval – Don't Panic Feb 01 '17 at 19:28
  • Seriously thought I was losing the plot! – Alan A Feb 01 '17 at 19:30
  • `echo $data->format('Y-m-d H:i:s');` inside foreach. check once. Rest Don't Panic is correct – Alive to die - Anant Feb 01 '17 at 19:31
  • DateTime object properties can't be directly accessed from within code, but are still visible for var_dump(), etc. This is a known [b̶u̶g̶ feature](https://bugs.php.net/bug.php?id=49382) – Mark Baker Feb 01 '17 at 19:33

0 Answers0