What are common pitfalls when iterating through objects in PHP?
One common pitfall when iterating through objects in PHP is trying to access object properties directly within a foreach loop. To avoid this issue, you should first convert the object to an array using the get_object_vars() function before iterating through it.
// Convert object to array before iterating
$object = new stdClass();
$object->name = 'John';
$object->age = 30;
$array = get_object_vars($object);
foreach ($array as $key => $value) {
echo $key . ': ' . $value . PHP_EOL;
}