Source: sebastiandedeyne.com

Named arguments
September 15, 2022 ∙ by https://sebastiandedeyne.com/ Look at this code, I have no idea what false, false, true conveys. $page->render(false, false, true); A pattern I often see in older code is an associative array as the single parameter.

class Page { private bool $showFooter = true; private bool $showHeader = true; private bool $includeAssets = false; public function showFooter(bool $showFooter): self { $this->showFooter = $showFooter; return $this; } public function showHeader(bool $showHeader): self { $this->showHeader = $showHeader; return $this; } public function includeAssets(bool $includeAssets): self { $this->includeAssets = $includeAssets; return $this; }

Proper IDE support, type safety, no internal state required, keeps the object’s public methods to a minimum.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners