加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP依赖倒置(Dependency Injection)代码实例

发布时间:2020-12-13 02:04:01 所属栏目:PHP教程 来源:网络整理
导读:《PHP实战:PHP依赖倒置(Dependency Injection)代码实例》要点: 本文介绍了PHP实战:PHP依赖倒置(Dependency Injection)代码实例,希望对您有用。如果有疑问,可以联系我们。 PHP进修 实现类: 代码如下: ?php ? class Container { ??? protected $setings

《PHP实战:PHP依赖倒置(Dependency Injection)代码实例》要点:
本文介绍了PHP实战:PHP依赖倒置(Dependency Injection)代码实例,希望对您有用。如果有疑问,可以联系我们。

PHP进修实现类:

代码如下:

<?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实战:PHP依赖倒置(Dependency Injection)代码实例》是否对您有启发,欢迎查看更多与《PHP实战:PHP依赖倒置(Dependency Injection)代码实例》相关教程,学精学透。编程之家 52php.cn为您提供精彩教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读