<?php
class Connection
{
public function query()
{
return new Result();
}
}
class Result implements \IteratorAggregate
{
private $rows;
public function __construct()
{
$this->rows = new \ArrayObject([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
]);
}
public function getIterator()
{
return $this->rows->getIterator();
}
}
$conn = new Connection();
// Here $result holds an instance of "Result"
// *** THIS IS YOUR CASE 2 ***
$result = $conn->query();
var_dump($result);
// "Result" is iterable, so we can do like this
foreach($result as $row)
{
var_dump($row);
}
// Or we can skip the intermediate variable and loop over the result immediately.
// *** THIS IS YOUR CASE 1 ***
foreach($conn->query() as $row)
{
var_dump($row);
}