在PHP中,依赖注入(Dependency Injection,简称DI)是一种设计模式,用于降低代码之间的耦合度。通过使用反射,我们可以在运行时动态地实例化和注入依赖。以下是一个简单的示例,展示了如何使用PHP反射实现依赖注入:
- 首先,创建一个接口和两个实现类:
interface MyInterface { public function doSomething(); } class MyImplementationA implements MyInterface { public function doSomething() { echo "MyImplementationA doSomething\n"; } } class MyImplementationB implements MyInterface { public function doSomething() { echo "MyImplementationB doSomething\n"; } }
- 创建一个容器类,用于处理依赖注入:
class Container { private $instances = []; public function set($key, $instance) { if (!is_object($instance)) { throw new InvalidArgumentException('Instance must be an object'); } $this->instances[$key] = $instance; } public function get($key) { if (!isset($this->instances[$key])) { throw new InvalidArgumentException('No instance found for key: ' . $key); } return $this->instances[$key]; } }
- 使用反射实现依赖注入:
function resolveDependencies(Container $container, array $typeHints) { $reflection = new ReflectionClass($typeHints['class']); $constructor = $reflection->getConstructor(); if ($constructor === null) { throw new InvalidArgumentException('Class ' . $typeHints['class'] . ' has no constructor'); } $dependencies = []; foreach ($constructor->getParameters() as $parameter) { $dependencyName = $parameter->getName(); if (!isset($container->get($dependencyName))) { throw new InvalidArgumentException('Dependency ' . $dependencyName . ' not found'); } $dependencies[] = $container->get($dependencyName); } return $reflection->newInstanceArgs($dependencies); }
- 使用依赖注入容器:
$container = new Container(); $container->set('myInterface', resolveDependencies($container, [ 'class' => MyInterface::class, 'dependencies' => [MyImplementationA::class, MyImplementationB::class], ])); $instance = $container->get('myInterface'); $instance->doSomething(); // Output: MyImplementationA doSomething
在这个示例中,我们创建了一个简单的依赖注入容器,它使用反射来动态地实例化和注入依赖。resolveDependencies
函数接受一个容器实例和一个类型提示数组,然后使用反射来获取类的构造函数和依赖项。接下来,它从容器中获取这些依赖项并使用它们来实例化类。最后,我们使用这个容器来实例化一个实现了MyInterface
接口的类,并调用其doSomething
方法。