返回 Skill 列表
extension
分类: 开发与工程无需 API Key

laravel:interfaces-and-di

使用接口和依赖注入来解耦代码;在容器中绑定实现

person作者: jakexiaohubgithub

Interfaces and Dependency Injection

Define narrow interfaces and inject them where needed. Bind concrete implementations in a service provider.

interface Slugger { public function slug(string $s): string; }

final class AsciiSlugger implements Slugger {
  public function slug(string $s): string { /* ... */ }
}

$this->app->bind(Slugger::class, AsciiSlugger::class);

Benefits: easier testing (mock interfaces), clearer contracts, swap implementations without touching consumers.