依赖注入和控制反转
在框架的底层设计中,需要很多类的协同工作,如果这些类之间依赖性很强,会出现许多的副作用。软件工程提倡的是高内聚,低耦合,为了降低类的耦合性,控制反转(IoC)是一种有效的设计原则,而依赖注入是控制反转的一种实现方式。
依赖注入例子:
<?php
namespace app\demo\controller;
use think\Request;
class Demo3
{
public function test(Request $request)
{
dump($request->get());
}
}
控制反转和依赖注入详解
Container类(也叫Container容器类)很重要,在使用控制反转前,类与类的依赖关系是杂乱无章的,而使用了Container容器类进行控制反转后,只有容器类依赖其他所有的类,而其他类之间没有了依赖关系,他们所需的实例都通过容器类进行依赖注入。
Container类的源码(看一部分就行):
```php
<?php
namespace think;
use Closure;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
class Container
{
protected static $instance;
protected $instances = [];
protected $bind = [];
public static function getInstance()
{
if (is_null(static::$instance)) {
static::$instance = new static;
}
return static::$instance;
}
public static function get($abstract, $vars = [], $newInstance = false)
{
return static::getInstance()->make($abstract, $vars, $newInstance);
}
public static function set($abstract, $concrete = null)
{
return static::getInstance()->bind($abstract, $concrete);
}
public function bind($abstract, $concrete = null)
{
if (is_array($abstract)) {
$this->bind = array_merge($this->bind, $abstract);
} elseif ($concrete instanceof Closure) {
$this->bind[$abstract] = $concrete;
} elseif (is_object($concrete)) {
$this->instances[$abstract] = $concrete;
} else {
$this->bind[$abstract] = $concrete;
}
return $this;
}
public function instance($abstract, $instance)
{
if (isset($this->bind[$abstract])) {
$abstract = $this->bind[$abstract];
}
$this->instances[$abstract] = $instance;
return $this;
}
public function bound($abstract)
{
return isset($this->bind[$abstract]) || isset($this->instances[$abstract]);
}
public function has($name)
{
return $this->bound($name);
}
public function make($abstract, $vars = [], $newInstance = false)
{
if (true === $vars) {
$newInstance = true;
$vars = [];
}
if (isset($this->instances[$abstract]) && !$newInstance) {
$object = $this->instances[$abstract];
} else {
if (isset($this->bind[$abstract])) {
$concrete = $this->bind[$abstract];
if ($concrete instanceof Closure) {
$object = $this->invokeFunction($concrete, $vars);
} else {
$object = $this->make($concrete, $vars, $newInstance);
}
} else {
$object = $this->invokeClass($abstract, $vars);
}
if (!$newInstance) {
$this->instances[$abstract] = $object;
}
}
return $object;
}
public function invokeFunction($function, $vars = [])
{
$reflect = new ReflectionFunction($function);
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($args);
}
public function invokeMethod($method, $vars = [])
{
if (is_array($method)) {
$class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);
$reflect = new ReflectionMethod($class, $method[1]);
} else {
$reflect = new ReflectionMethod($method);
}
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs(isset($class) ? $class : null, $args);
}
public function invoke($callable, $vars = [])
{
if ($callable instanceof Closure) {
$result = $this->invokeFunction($callable, $vars);
} else {
$result = $this->invokeMethod($callable, $vars);
}
return $result;
}
public function invokeClass($class, $vars = [])
{
$reflect = new ReflectionClass($class);
$constructor = $reflect->getConstructor();
if ($constructor) {
$args = $this->bindParams($constructor, $vars);
} else {
$args = [];
}
return $reflect->newInstanceArgs($args);
}
protected function bindParams($reflect, $vars = [])
{
$args = [];
if ($reflect->getNumberOfParameters() > 0) {
reset($vars);
$type = key($vars) === 0 ? 1 : 0;
$params = $reflect->getParameters();
foreach ($params as $param) {
$name = $param->getName();
$class = $param->getClass();
if ($class) {
$className = $class->getName();
$args[] = $this->make($className);
} elseif (1 == $type && !empty($vars)) {
$args[] = array_shift($vars);
} elseif (0 == $type && isset($vars[$name])) {
$args[] = $vars[$name];
} elseif ($param->isDefaultValueAvailable()) {
$args[] = $param->getDefaultValue();
} else {
throw new InvalidArgumentException('method param miss:' . $name);
}
}
}
return $args;
}
}
|