Questions tagged [php-di]

PHP-DI is a simple to use dependency injection container for PHP. It is practical, powerful, and framework-agnostic.

PHP-DI is a simple to use dependency injection container for PHP. It is practical, powerful, and framework-agnostic.

See more:

101 questions
2
votes
1 answer

how to inject reusable controller injection with php-di correctly

I'm starting a new slim 4 application, and current slim versions are more just a router than a framework and do not have di container out of the box, so I choose PHP-DI as a DI container. And to not describe each controller in the container and to…
Bogdan Dubyk
  • 4,756
  • 7
  • 30
  • 67
2
votes
1 answer

Dependency injection cannot be handled using php-di

This is a problem that drove me crazy for over one week. I installed php-di via composer and added my own project to the composer.json file: { "name": "mypackage/platform", "description": "MyPackage Platform", "type": "project", …
Belkin
  • 199
  • 15
2
votes
0 answers

Definitions in dependency injection via php-di

Getting my head wrapped around dependency injection with php-di, but I'm obviously not doing something right. Wondering if someone could tell me what I'm doing wrong. Ok, have a bootstrap.php file, no namespace in it, and looks something…
Envrin
  • 19
  • 1
2
votes
3 answers

php-di how to access container definitions

i have some config file which return array of key-value return [ 'my_key' => 'my value', ]; i have added it to the php-di container as definition $builder->addDefinitions(dirname(__DIR__) . '/config.php'); $container =…
Eugene
  • 1,680
  • 3
  • 14
  • 23
2
votes
1 answer

Inject mock objects using PHP-DI for testing controllers with PHPUnit

I have refactored some of my controllers to use dependency injection via property injection as recommended in the "best practices": final class ZebraController extends Controller { /** * @Inject * @var AnimalClientInterface */ …
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
2
votes
1 answer

PHP DI and casting interface

I do not understand, for example I'm using php-di container, I have ClassA and ClassB, both of them are implements Intreface1 And I'm creating objects of this two classes thru DI Now when I'm injecting ClassA to the constructor I need inject…
2
votes
1 answer

PHP-DI: create vs. autowire vs. get. Or how to correctly map an interface to an implementation provided by a definition

For my PHP-DI container in a web MVC I prepared the following definition for a router: return [ 'router' => function (ContainerInterface $c) { //... return new Router(...); } ]; Now I want to map an interface…
PajuranCodes
  • 303
  • 3
  • 12
  • 43
2
votes
2 answers

PHP-DI cannot be resolved: Entry cannot be resolved: Parameter xxxxxx of __construct() has no value defined or guessable Full definition

Just throwing this out here because I couldn't find a lot of info on this error and it took me about 2 hours to find it. face palm In container.php -> DBService defined as: DBServiceInterface::class => function (ContainerInterface $c) { …
Aaron Bar
  • 611
  • 1
  • 7
  • 20
2
votes
1 answer

Using PHP-DI for Wordpress Plugin using PSR-4?

I'm writing a wordpress plugin using the https://github.com/DevinVinson/WordPress-Plugin-Boilerplate template and I'm trying to configure PHP-DI (http://php-di.org/) to handle injection of classes across my plugin. My composer config is this { …
emeraldjava
  • 10,894
  • 26
  • 97
  • 170
2
votes
1 answer

PHP-DI: Routes and optional segments

I am using PHP-DI with the Slim framework. In Slim you make a route segment optional by putting brackets around it. Now that I have set up PHP-DI when I leave out the optional route segment, I get this error: Type:…
donfifty
  • 322
  • 2
  • 13
2
votes
1 answer

Multiple constructor parameter injection in php-di

I'm trying to use PHP-DI, but I did not quite succeed. In my simple scanario, a controller, in a Wordpress Theme, needs a PostService and a CategoryService injected in constructor: class IndexController extends ChesterBaseController { private…
ale
  • 10,012
  • 5
  • 40
  • 49
2
votes
0 answers

PHP constructor based dependency inject vs new. Which one is more efficient?

There are two types of Dependency injection http://www.javatpoint.com/dependency-injection-in-spring Constructor-based dependency injection Setter-based dependency injection However Which one of the following is more efficient and why? or are they…
Manjunath Reddy
  • 1,039
  • 2
  • 13
  • 22
2
votes
1 answer

Why to use Dependency Injection components in PHP frameworks

When I first saw dependency injection components like PHP-DI, Symfony2 DI, etc., I though, there is a way to automatically inject instance of any class to any just with one instantiation. So 1. Create instance in root class like $foo = new Foo() 2.…
LordMagik
  • 43
  • 10
1
vote
2 answers

I'm trying to learn Slim 4 and how to add Twig to it using PHP-DI but not able to figure it out

So I'm using a skeleton I found online.... The "boostrap.php" looks like use DI\ContainerBuilder; use Slim\App; require_once __DIR__ . '/../vendor/autoload.php'; $containerBuilder = new ContainerBuilder(); // Add DI container…
BostonMacOSX
  • 1,369
  • 2
  • 17
  • 38
1
vote
0 answers

Mock a certain constructor argument using PHP-DI and PHPUnit

I'm using PHP-DI to inject classes into the constructor as follows class StravaWebhookService extends ApplicationService { private ContainerInterface $container; private UserService $userService; private ActivityService…
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125