一键导入
dependency-injection
Use dependency injection cleanly in PHP applications. Constructor injection, avoiding service locators, what to put in the container.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use dependency injection cleanly in PHP applications. Constructor injection, avoiding service locators, what to put in the container.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Add modern PHP code to a legacy 7.x codebase incrementally. Typed properties, readonly, enums — how to introduce them without forcing a big-bang upgrade.
Author a boost.php config file. Covers all five with* methods, when each matters, and what NOT to put in there.
Implement a FileEmitter for boost-core to emit a custom file (e.g. .mcp.json, .editorconfig) into the host project during boost:sync.
| name | dependency-injection |
| description | Use dependency injection cleanly in PHP applications. Constructor injection, avoiding service locators, what to put in the container. |
new SomeService() deep in a methodfinal class CreateOrder
{
public function __construct(
private readonly OrderRepository $orders,
private readonly EventDispatcher $events,
private readonly Clock $clock,
) {}
}
Promoted constructor params (PHP 8.0+). Readonly (PHP 8.1+). Typed against interfaces, not concrete classes (testability).
new. new EmailAddress($input).// AVOID
public function handle(): void
{
$service = app(SomeService::class); // service locator
// ...
}
// PREFER
public function __construct(private readonly SomeService $service) {}
public function handle(): void
{
$this->service->doIt();
}
The only legitimate use of a service locator is at the framework boundary (routing, command dispatch). Below that line: constructor injection.
new SomeService() inside business logic → not testable in isolationConstructor injection makes test fakes trivial:
$cmd = new CreateOrder(
orders: new InMemoryOrderRepository(),
events: new RecordingDispatcher(),
clock: new FrozenClock('2026-01-15'),
);
No container, no mocks, no magic.