Update SelectQuery.php
🔍 What was changed
Adding fetch() simplifies common one-row use cases, makes intent explicit, and avoids the perf overhead of materialising an unused array.
🤔 Why?
-
Clearer intent fetch() makes it obvious we expect one record (or null). Code reviewers no longer have to parse an indexing expression to know this.
-
Avoids unnecessary allocations fetchAll() builds a PHP array for every row returned by the database driver. When the caller only needs the first row, we: allocate and populate an array for all rows, immediately destroy most of it, pay the GC cost later.
fetch() stops reading after the first result, closes the cursor in the finally, and releases the driver buffer early. This is a measurable win on large result sets and in tight loops.
- Reduces boilerplate and error-prone code No more [0] ?? null, no more manual casts, no more “did we forget to check the array isn’t empty?” bugs. The new method returns:
an array representing the row (default FETCH_ASSOC), or false when the result set is empty (mirrors PDO).
- How was this tested:
- [X] Tested manually
- [ ] Unit tests added
I like the idea. Need to think about the best name for the method: fetch(), fetchOne(), first(), fetchFirst().
Still need to add tests
I just keep the sema logic with fetchAll and fetch from the pdo