<?php
require_once("vendor/autoload.php");
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Injector\Injector;
class ProcessOrderCommand
{
public function __construct(Db $db, Curl $curl) {}
}
class Db {}
class Curl {}
$config = ContainerConfig::create();
$container = new Container($config);
$injector = new Injector($container);
$obj = $injector->make(ProcessOrderCommand::class);
I think “code to an interface, not an implementation” is the one, eh? On the other hand, sometimes the probability of switching the implementation is very, very low, so adding another layer of complexity might not be worth it. Not that an interface is so complicated…
PHPUnit should easily be able to do mocking of classes too, not only interfaces, IIRC.
Hm getting confused again. Isn’t this support to work?
Using DI 1.1 and injector 1.2 due to PHP 7.4 constraints.
<?php
require_once("vendor/autoload.php");
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Injector\Injector;
$diConfig = ContainerConfig::create()->withDefinitions(['\Foo' => fn() => new stdClass()]);
$container = new Container($diConfig);
$injector = new Injector($container);
$db = $injector->make('\Foo');
Getting error
PHP Fatal error: Uncaught ReflectionException: Class “\Foo” does not exist