MNB PHPExcelDeveloper Guide
Core concepts

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
LevelBeginnerReading time9 minPackagemnb/mnb-phpexcel or a format package
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#

PHP
$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#

MethodReturnsUse it when
rows()GeneratorYou want forward-only row processing.
toArray()arrayYou need all returned rows in memory.
first()?arrayYou need only the first normalized data row.
countRows()intYou need the final normalized row count.
hasRows()boolYou only need to know whether data exists.
toStructuredSheetArray()arrayYou need rows plus source, empty, header, and returned-row counts.
cell("B2")mixedYou need one direct worksheet coordinate.

Correct and incorrect chaining#

PHP
$data = $session->toArray();
$data->rows();

Avoid accidental repeated passes#