
PHP Enums Are Not Your Bottleneck (Here's Proof)
When you're building a large export - say, 50_000 order items, you start looking at every part of the code and wondering: what's slowing this down? Here we have some order item export simplified code: final class OrderItemExport { public function store (): void { $rows = $this -> query () -> toBase () -> cursor (); foreach ( $rows as $row ) { $this -> writer -> addRow ( $this -> map ( $row )); } $this -> writer -> close (); } private function map ( object $row ): array { $status = OrderItemStatusEnum :: tryFrom ( $row -> status_id ); $currency = CurrencyEnum :: tryFrom ( $row -> currency_id ); return [ $row -> order_id , $status ?-> label (), $currency ?-> code (), ]; } } Enums are an easy target. They look like objects, they have methods, and you call them on every row. So the question came up naturally: do PHP enums create overhead in a loop like this? $orderItemStatus = OrderItemStatusEnum :: tryFrom ( $row -> status_id ); $statusLabel = $orderItemStatus ?-> label (); Let's find out
Continue reading on Dev.to
Opens in a new tab




