So I’m kinda stuck wrapping my head around this whole parent-child dependency thing. My assumption was, once you declare all the dependencies in the parent (BaseController), the child (like IndexController) wouldn’t need to declare them again in its own constructor; it could just rely on parent::__construct(). Tried removing the four dependencies from IndexController, but got hit with an error. Basically, you still gotta re-declare those four deps in the child’s constructor so they can be passed up to parent::__construct(). At least that's what the AI told me. Lastly, are those use statements really necessary to be in every file?
BaseController.php
<?php use App\Models\SiteSettings; use App\Services\{ AuthService, LoggerService, MessageService }; class BaseController { public function __construct( protected SiteSettings $siteSettings, protected AuthService $authService, protected LoggerService $loggerService, protected MessageService $messageService ) {} } IndexController.php
<?php use App\Models\SiteSettings; use App\Services\{ AuthService, LoggerService, MessageService }; class IndexController extends BaseController { public function __construct( SiteSettings $site_settings, AuthService $auth_service, LoggerService $logger_service, MessageService $message_service, private ServersRepository $serversRepository, private ServerFilter $serverFilter, private CategoriesRepository $categoriesRepository, private LanguagesRepository $languagesRepository ) { parent::__construct( $site_settings, $auth_service, $logger_service, $message_service ); } $this->messageService->get('MSG_LOGIN_SUCCESS'); } Container.php
<?php use App\Models\SiteSettings; use App\Services\{ AuthService, LoggerService, MessageService }; class Container { public function __construct() { $this->factories[IndexController::class] = function (Container $c) { return new IndexController( $c->get(SiteSettings::class), $c->get(AuthService::class), $c->get(LoggerService::class), $c->get(MessageService::class), $c->get(ServersRepository::class), $c->get(ServerFilter::class), $c->get(CategoriesRepository::class), $c->get(LanguagesRepository::class) ); }; }
new IndexControllerit is your burden to satisfy the requirements. If you are using a framework it is possible that it has a pattern to resolve this for you automatically. For instance, instead of manually instantiating something you'd instead ask the framework for an instance for you, and it resolves the dependencies on your behalf.