以下のような場合、
ユーザのコントローラ: UserController
ユーザのサービスクラス: UserService
ユーザのリポジトリクラス: UserRepository
class UserService { private $userRepository public function __construct( UserRepository $userRepository, ) { $this->userRepository = $userRepository; } }
UserServiceクラスでは、UserRepositoryクラスをコンストラクタインジェクションしています。
このUserServiceクラスを、コントローラでメソッドインジェクションしたいのですが、コンストラクタで引数をとっているため、うまくいきません。
class UserController extends Controller { public function create(UserService $userService) { // ここでuserServiceを使用したい。 } }
なにか良い方法はありますでしょうか?
UserServiceクラスでUserRepositoryはDIして使用したいです。
そもそもコンストラクタインジェクションはあまり使用しないのでしょうか?
ベストプラクティスみたいなものがあれば教えていただきたく思います。
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。