Strange behavior when debugging Call in member function format () in boolean

tl; dr When iterating over an array of 1000 objects containing a DateTime property and resetting that property, I get 1000 valid DateTime objects in response. When a feature is added ->format

on a subsequent dump line, termination is immediately terminated at the first item. Adding a call format()

outside of the dump statement stops completion at the 1000th element.


In my php project (symfony) I am experiencing the following error:

Call in member function () format on boolean

This would normally be easy to fix, but while debugging I ran into some strange behavior.

private function filterDuplicates(array $data) {
  $duplicateMap = [];

  foreach($data as $index => $item) {
    // dump here, see below
    $hashKey = md5($item->getBracelet()->getId() . $item->getTimestamp()->format('d/m/y H:i'));
    if (isset($duplicateMap[$hashKey])) {
      unset($data[$index]);
      unset($data[$duplicateMap[$hashKey]]);
    } else {
      $duplicateMap[$hashKey] = $index;
    }
  }

  return $data;
}

      

This code is currently giving me a format error where I am trying to set the $ hashkey variable. Apparently one of the $ item objects contains an invalid DateTime, or so I thought.

On adding, dump($item->getTimeStamp());

I get exactly 1000 valid DateTime objects followed by a format error (). I originally assumed this meant that the error occurred on the last object in the array.

However, my array only contains 1000 objects, which I tested with dump(count($data));

.

Odd. But that's okay. I rewrote its landfill, as such: dump($item->getTimestamp() instanceof \DateTime)

. This is printed 1000 times true

. Everything seems to work.

Now for the weird part.

Now I changed my dump to this:

dump($index);
$hashKey = md5($item->getBracelet()->getId() . $item->getTimestamp()->format('d/m/y H:i'));

      

And got this output:

...
996
997
998
999
PHP Fatal error:  Call to a member function format() on boolean in /Users/svenhoskens/Documents/web_projects/Tomorrowland_api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php on line 53

      

HOWEVER , changing that to the following:

dump($index);
dump($item->getTimestamp()->format('d/m/y H:i'));
$hashKey = md5($item->getBracelet()->getId() . $item->getTimestamp()->format('d/m/y H:i'));

      

I got this weird output:

0
PHP Fatal error:  Call to a member function format() on boolean in /Users/svenhoskens/Documents/web_projects/Tomorrowland_api/src/AdminPanelBundle/Command/ProcessDatafileCommand.php on line 374

      

As you can see, instead of looping through 1000 objects and throwing an error at the end, php now throws an error at the very beginning, although my dump is immediately followed by the same format call that causes the error

What am I missing here? What can cause this behavior?

+3


source to share


1 answer


The issue is currently resolved, although I'm afraid I don't know why. After completely restarting my machine, the problem cannot be reproduced again. The loop now fails at the correct time regardless of the content or order of the dump commands. If anyone comes across something like this again, let me know.



0


source







All Articles