简体   繁体   中英

PHP Dependency Injection Management

I have Created a PHP package where I applied solid principles, which was great, however I ended up having many separate dependencies to be injected in the main class or module which will be then used.

example :

$dataViewer   = new DataViewer;
$dataProvider = new DataProvider;
$dataParser   = new DataParser;
$dataSaver    = new DataSaver;

... and then use all these dependencies in my main module or class like so:

$myPackage = new MyPackage($dataViewer, $dataProvider, $dataParser, $dataSaver);

Then I use my package.

Is there a way to make this a little bit easier for those who use this package and need only to get an instance of MyPackage instead of injecting all these dependencies.

This comment could help you. Remember that there are a lot of ways to inject dependencies, maybe by setters/getters?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM