Suppose I have the following class structure:
class Logger { public function __construct(string $channel) { // Logger初始化工作 } } class UsingLogger { private Logger $logger; public function __construct(Logger $logger) { $this->logger = $logger; } } $logger = new Logger("UsingLogger"); $usingLogger = new UsingLogger($logger);
This code works fine. In this case, the name of the channel is the name of the class. However, I want to use PHP DI (https://php-di.org/doc/php-definitions.html#autowired-objects) to solve this problem. The problem is that it can't resolve the situation because it doesn't know the class name passed to the logger.
Example PhpDI definition
return [ "SomeLoggerInterface" => autowire(Logger::class)->constructorParameter("channel", // 在这里获取类的名称。在这种情况下,它将是 "UsingLogger") ]
Hope this makes it clear. If more context is needed, please let me know.
I have outlined my steps
I assume you have the DI package installed.
Running this script will produce the following output:
Maybe you should also use interfaces. Hope this helps you.