douyaju4259 2017-07-04 15:57
浏览 66

调试时的奇怪行为在布尔值上调用成员函数format()

tl;dr When iterating over an array of 1000 objects containing a DateTime property and dumping this property, I get 1000 valid DateTime objects in response. When adding a ->format can on a subsequent dump line, termination stops immediately at the first element. Adding a format() call outside of a dump statement only stops termination at the 1000th element.


I'm currently experiencing the following error in my php (symfony) project:

Call to a member function format() on boolean

Normally this would be an easy fix, but I've encountered some strange behaviour while debugging.

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 currently gives me the format error right where I'm attempting to set the $hashkey variable. Apparently, one of the $item objects contains an invalid DateTime, or so I thought.

When adding a dump($item->getTimeStamp());, I get exactly 1000 correct DateTime objects, followed by the same format() error. Initially, I figured this meant that the error occured on the latest object in the array.

However, my array only contains exactly 1000 objects, which I have tested using dump(count($data));.

Odd. But okay. I rewrote my dump as such: dump($item->getTimestamp() instanceof \DateTime). This printed 1000 times true. Everything seems like it should be working.

Now for the strange part.

I now changed my dump to this:

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

And received 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, upon changin that to this:

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 strange 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 over the 1000 objects and throwing an error at the end, php now throws the error at the very start, even though my dump is immediately followed by the very same format call which causes the error!

What am I missing here? What could cause such a behaviour?

  • 写回答

1条回答 默认 最新

  • doushijiao0679 2017-07-05 11:56
    关注

    The issue is currently resolved, though I'm afraid I don't know why. After a complete restart of my machine, the issue could not be reproduced again. The loop now fails at the correct time, regardless of the contents or order of the dump commands. If anyone encounters something like this again, let me know.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比