PHP依賴(lài)倒置(Dependency Injection)代碼實(shí)例
更新時(shí)間:2014年10月11日 12:08:16 作者:十年燈
這篇文章主要介紹了PHP依賴(lài)倒置(Dependency Injection)代碼實(shí)例本文只提供實(shí)現(xiàn)代碼,需要的朋友可以參考下
實(shí)現(xiàn)類(lèi):
復(fù)制代碼 代碼如下:
<?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;
}
}
實(shí)現(xiàn)實(shí)例:
復(fù)制代碼 代碼如下:
<?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);
您可能感興趣的文章:
- 詳解Java設(shè)計(jì)模式編程中的依賴(lài)倒置原則
- 深入理解JavaScript系列(22):S.O.L.I.D五大原則之依賴(lài)倒置原則DIP詳解
- MVC使用Spring.Net應(yīng)用IOC(依賴(lài)倒置)學(xué)習(xí)筆記3
- PHP面向?qū)ο笪宕笤瓌t之里氏替換原則(LSP)詳解
- PHP面向?qū)ο笪宕笤瓌t之接口隔離原則(ISP)詳解
- PHP面向?qū)ο笪宕笤瓌t之開(kāi)放-封閉原則(OCP)詳解
- PHP面向?qū)ο笪宕笤瓌t之單一職責(zé)原則(SRP)詳解
- PHP基于面向?qū)ο髮?shí)現(xiàn)的留言本功能實(shí)例
- PHP面向?qū)ο笪宕笤瓌t之依賴(lài)倒置原則(DIP)詳解
相關(guān)文章
thinkphp5.1 中使用自定義異常處理類(lèi)進(jìn)行接管
這篇文章主要介紹了thinkphp5.1 中使用自定義異常處理類(lèi)進(jìn)行接管,本文通過(guò)配置文件的修改和具體代碼實(shí)現(xiàn)詳細(xì)展開(kāi)的講解了如何使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Yii基于數(shù)組和對(duì)象的Model查詢(xún)技巧實(shí)例詳解
這篇文章主要介紹了Yii基于數(shù)組和對(duì)象的Model查詢(xún)技巧,結(jié)合實(shí)例形式較為詳細(xì)的分析了Yii針對(duì)數(shù)組及對(duì)象的Model查詢(xún)使用技巧,需要的朋友可以參考下2015-12-12
微信小程序 消息推送php服務(wù)器驗(yàn)證實(shí)例詳解
這篇文章主要介紹了微信小程序 消息推送php服務(wù)器驗(yàn)證實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
thinkPHP訂單數(shù)字提醒功能的實(shí)現(xiàn)方法
這篇文章主要介紹了thinkPHP訂單數(shù)字提醒功能的實(shí)現(xiàn)方法,涉及thinkPHP數(shù)據(jù)庫(kù)查詢(xún)、遍歷及前臺(tái)顯示相關(guān)功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12

