PHP依賴倒置(Dependency Injection)代碼實(shí)例
更新時(shí)間:2014年10月11日 12:08:16 作者:十年燈
這篇文章主要介紹了PHP依賴倒置(Dependency Injection)代碼實(shí)例本文只提供實(shí)現(xiàn)代碼,需要的朋友可以參考下
實(shí)現(xiàn)類:
復(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ì)模式編程中的依賴倒置原則
- 深入理解JavaScript系列(22):S.O.L.I.D五大原則之依賴倒置原則DIP詳解
- MVC使用Spring.Net應(yīng)用IOC(依賴倒置)學(xué)習(xí)筆記3
- PHP面向?qū)ο笪宕笤瓌t之里氏替換原則(LSP)詳解
- PHP面向?qū)ο笪宕笤瓌t之接口隔離原則(ISP)詳解
- PHP面向?qū)ο笪宕笤瓌t之開放-封閉原則(OCP)詳解
- PHP面向?qū)ο笪宕笤瓌t之單一職責(zé)原則(SRP)詳解
- PHP基于面向?qū)ο髮?shí)現(xiàn)的留言本功能實(shí)例
- PHP面向?qū)ο笪宕笤瓌t之依賴倒置原則(DIP)詳解
相關(guān)文章
PHP微信開發(fā)之微信消息自動(dòng)回復(fù)下所遇到的坑
這篇文章是小編給大家介紹的微信消息自動(dòng)回復(fù)下所遇到的坑的相關(guān)內(nèi)容,在日常項(xiàng)目開發(fā)中經(jīng)常遇到,非常具有參考借鑒價(jià)值,感興趣的小伙伴一起學(xué)習(xí)吧2016-05-05php的sprintf函數(shù)的用法 控制浮點(diǎn)數(shù)格式
這篇文章主要介紹了php的sprintf函數(shù)的用法,需要的朋友可以參考下2014-02-02php jquery 多文件上傳簡(jiǎn)單實(shí)例
這篇文章主要介紹了php jquery 多文件上傳簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12php實(shí)現(xiàn)生成驗(yàn)證碼實(shí)例分享
由于注冊(cè)的時(shí)候常常會(huì)用到注冊(cè)碼來防止機(jī)器惡意注冊(cè),這里我發(fā)表一個(gè)產(chǎn)生圖片驗(yàn)證碼的基本圖像,很簡(jiǎn)陋,有需要的小伙伴可以參考下2016-04-04PHP面向?qū)ο笾I(lǐng)域模型+數(shù)據(jù)映射器實(shí)例(分析)
下面小編就為大家?guī)硪黄狿HP面向?qū)ο笾I(lǐng)域模型+數(shù)據(jù)映射器實(shí)例(分析)。小編覺得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06laravel框架之?dāng)?shù)據(jù)庫(kù)查出來的對(duì)象實(shí)現(xiàn)轉(zhuǎn)化為數(shù)組
今天小編就為大家分享一篇laravel框架之?dāng)?shù)據(jù)庫(kù)查出來的對(duì)象實(shí)現(xiàn)轉(zhuǎn)化為數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10