use DI\ContainerBuilder; $builder = new ContainerBuilder(); $builder->addDefinitions([ 'database.host' => 'localhost', 'database.user' => 'root', 'database.pass' => 'password', ]); $container = $builder->build();
use DI\Container; class UserController { protected $container; public function __construct(Container $container) { $this->container = $container; } public function view($id) { $user = $this->container->get(UserRepository::class)->findById($id); return view('user.view', compact('user')); } }In this example, the UserController class is instantiated with a Container object that allows it to access the UserRepository class (which is also instantiated via the container). This makes it easier to test the UserController, as the dependencies can be mocked or replaced as needed. Overall, PHP Static DI is a powerful technique that can make your code more flexible and maintainable. By using a package like PHP-DI, you can take advantage of this approach without having to write complex DI code from scratch.