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 (!