Source: juststeveking-uk-astro.pages.dev

Adapter Pattern
Of all of the design patterns you could use in your code, the adapter pattern is one of my all time favourites. so you can switch implementation simply by switching the adapter.

class TwitterAdapter implements NetworkAdapterContract { public function __construct( private TwitterSDK $sdk, ) {} public function post(string $message): void { $this->sdk->tweet(Formatter::tweet($message)); } public function fetch(mixed $identifier): mixed { return $this->sdk->fetchTweet($identifier); } public function delete(mixed $identifier): void { $this->ssk->deleteTweet($identifier); } public function get(): array { return $this->sdk->getLatestTweets(); }}

class Poster { private function __construct( private string $adapter, ) {} public static function use( string $adapter, ): static { return new static( adapter: $adapter, ); }}

However, if you have a very complicated implementation that you don’t want to duplicate methods you can use the __call() method: class Poster { private function __construct( private string $driver, private NetworkAdapterContract $adapter, ) {} public static function use( string $driver, ): static { return new static( driver: $driver, adapter: app()->make($driver), ); } public function __call($method, $args) { if (!
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