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?