Fluent and Terminal Methods
Understand which reading calls keep configuring a ReadSession and which calls execute work and return generators, arrays, counts, rows, or cell values.
Updated
Full packageIndividual packagesReadSession
ReadSession::sheet()ReadSession::withHeaderRow()ReadSession::withOptions()ReadSession::rows()ReadSession::toArray()ReadSession::first()What you will learn
- Keep configuration calls on a read session.
- Recognize the call that executes reading and changes the return type.
- Avoid calling session methods on native arrays or generators.
Fluent methods keep building the session#
$session = MnbExcel::read('orders.xlsx')
->sheet('Orders')
->withHeaderRow(1)
->selectColumns(['Order ID', 'Customer'])
->withOptions(['skip_empty_rows' => true]);- Returns
ReadSession- Reads rows immediately
- No. These calls define what later terminal operations will read.
- Safe to continue chaining
- Yes.
- Examples
sheet(),withHeaderRow(),selectColumns(),withOptions(),skip(),limit().
Terminal methods execute work and return another type#
| Method | Returns | Use it when |
|---|---|---|
rows() | Generator | You want forward-only row processing. |
toArray() | array | You need all returned rows in memory. |
first() | ?array | You need only the first normalized data row. |
countRows() | int | You need the final normalized row count. |
hasRows() | bool | You only need to know whether data exists. |
toStructuredSheetArray() | array | You need rows plus source, empty, header, and returned-row counts. |
cell("B2") | mixed | You need one direct worksheet coordinate. |
Correct and incorrect chaining#
$data = $session->toArray();
$data->rows();$data = $session->toArray();
foreach ($data as $row) {
print_r($row);
}foreach ($session->rows() as $row) {
print_r($row);
}Avoid accidental repeated passes#
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue