PHP依赖倒置(Dependency Injection)代码实例
程序员文章站
2022-04-26 12:11:34
实现类:
复制代码 代码如下:
<?php
class container
{
protected $setings = array();
public function set($abstract, $concrete = null)
{
if ($concrete === null) {
$concrete = $abstract;
}
$this->setings[$abstract] = $concrete;
}
public function get($abstract, $parameters = array())
{
if (!isset($this->setings[$abstract])) {
return null;
}
return $this->build($this->setings[$abstract], $parameters);
}
public function build($concrete, $parameters)
{
if ($concrete instanceof closure) {
return $concrete($this, $parameters);
}
$reflector = new reflectionclass($concrete);
if (!$reflector->isinstantiable()) {
throw new exception("class {$concrete} is not instantiable");
}
$constructor = $reflector->getconstructor();
if (is_null($constructor)) {
return $reflector->newinstance();
}
$parameters = $constructor->getparameters();
$dependencies = $this->getdependencies($parameters);
return $reflector->newinstanceargs($dependencies);
}
public function getdependencies($parameters)
{
$dependencies = array();
foreach ($parameters as $parameter) {
$dependency = $parameter->getclass();
if ($dependency === null) {
if ($parameter->isdefaultvalueavailable()) {
$dependencies[] = $parameter->getdefaultvalue();
} else {
throw new exception("can not be resolve class dependency {$parameter->name}");
}
} else {
$dependencies[] = $this->get($dependency->name);
}
}
return $dependencies;
}
}
<?php
require 'container.php';
interface myinterface{}
class foo implements myinterface{}
class bar implements myinterface{}
class baz
{
public function __construct(myinterface $foo)
{
$this->foo = $foo;
}
}
$container = new container();
$container->set('baz', 'baz');
$container->set('myinterface', 'foo');
$baz = $container->get('baz');
print_r($baz);
$container->set('myinterface', 'bar');
$baz = $container->get('baz');
print_r($baz);
实现类:
复制代码 代码如下:
<?php
class container
{
protected $setings = array();
public function set($abstract, $concrete = null)
{
if ($concrete === null) {
$concrete = $abstract;
}
$this->setings[$abstract] = $concrete;
}
public function get($abstract, $parameters = array())
{
if (!isset($this->setings[$abstract])) {
return null;
}
return $this->build($this->setings[$abstract], $parameters);
}
public function build($concrete, $parameters)
{
if ($concrete instanceof closure) {
return $concrete($this, $parameters);
}
$reflector = new reflectionclass($concrete);
if (!$reflector->isinstantiable()) {
throw new exception("class {$concrete} is not instantiable");
}
$constructor = $reflector->getconstructor();
if (is_null($constructor)) {
return $reflector->newinstance();
}
$parameters = $constructor->getparameters();
$dependencies = $this->getdependencies($parameters);
return $reflector->newinstanceargs($dependencies);
}
public function getdependencies($parameters)
{
$dependencies = array();
foreach ($parameters as $parameter) {
$dependency = $parameter->getclass();
if ($dependency === null) {
if ($parameter->isdefaultvalueavailable()) {
$dependencies[] = $parameter->getdefaultvalue();
} else {
throw new exception("can not be resolve class dependency {$parameter->name}");
}
} else {
$dependencies[] = $this->get($dependency->name);
}
}
return $dependencies;
}
}
实现实例:
复制代码 代码如下:
<?php
require 'container.php';
interface myinterface{}
class foo implements myinterface{}
class bar implements myinterface{}
class baz
{
public function __construct(myinterface $foo)
{
$this->foo = $foo;
}
}
$container = new container();
$container->set('baz', 'baz');
$container->set('myinterface', 'foo');
$baz = $container->get('baz');
print_r($baz);
$container->set('myinterface', 'bar');
$baz = $container->get('baz');
print_r($baz);
上一篇: 三个人的幽默大家的开心
推荐阅读
-
PHP依赖倒置(Dependency Injection)代码实例
-
PHP控制反转(IOC)和依赖注入(DI)的实例代码详解
-
PHP依赖倒置(Dependency Injection)代码实例_PHP
-
PHP依赖倒置(Dependency Injection)代码实例,依赖倒置原则_PHP教程
-
PHP依赖倒置(Dependency Injection)代码实例_PHP
-
PHP依赖倒置(Dependency Injection)代码实例,依赖倒置原则
-
PHP依赖倒置(Dependency Injection)代码实例_php实例
-
关于PHP的依赖倒置(Dependency Injection)
-
PHP依赖倒置(Dependency Injection)代码实例_php实例
-
PHP依赖倒置(Dependency Injection)代码实例