自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP如何實(shí)現(xiàn)依賴注入

開發(fā) 后端
控制反轉(zhuǎn)(Inversion of Control,英文縮寫為IoC)是框架的重要特征??刂品崔D(zhuǎn)(IOC)是一種思想,依賴注入(DI)是實(shí)施這種思想的方法。

[[200267]]

摘要: 控制反轉(zhuǎn)(Inversion of Control,英文縮寫為IoC)是框架的重要特征??刂品崔D(zhuǎn)(IOC)是一種思想,依賴注入(DI)是實(shí)施這種思想的方法。

高層模塊不應(yīng)該依賴于底層模塊,兩個(gè)都應(yīng)該依賴抽象。

抽象不應(yīng)該依賴于細(xì)節(jié),細(xì)節(jié)應(yīng)該依賴于抽象。

首先,我們來看一段代碼:

  1. class A{ 
  2.         public function echo() 
  3.         { 
  4.                 echo 'A'.PHP_EOL; 
  5.         } 
  6. class EchoT { 
  7.         protected  $t; 
  8.         public function __construct() 
  9.         { 
  10.               $this->t = new A(); 
  11.         } 
  12.         public function echo(){ 
  13.                 $this->t->echo(); 
  14.         } 
  15.  

初始,我們都使用new 的方式在內(nèi)部進(jìn)行,EchoT類嚴(yán)重依賴于類A。每當(dāng)類A變化時(shí),EchoT類也得進(jìn)行變化。

我們優(yōu)化一下代碼

  1. class EchoT { 
  2.         protected  $t; 
  3.         public function __construct($t)  //構(gòu)造器注入由構(gòu)造器注入到其中 
  4.         { 
  5.               $this->t = $t; 
  6.         }  

可以看到,這樣做的話。很大程序上,我們對(duì)程序進(jìn)行了解耦。類A無論你如何變動(dòng),EchoT類是不需要變動(dòng)的。不再依賴于A。但是新問題又來了,我們現(xiàn)在只有A,萬一來了B,來了CDEFG怎么辦。

面向接口 

  1. interface T{ 
  2.         public function echo(); 
  3.  
  4. class A{ 
  5.         public function echo() 
  6.         { 
  7.                 echo 'A'.PHP_EOL; 
  8.         } 
  9.  
  10. class B implements T{ 
  11.         public function echo() 
  12.         { 
  13.                 echo 'B'.PHP_EOL; 
  14.         } 
  15. class EchoT { 
  16.         protected  $t; 
  17.         public function __construct(T $t)  //構(gòu)造器注入由構(gòu)造器注入到其中 
  18.         { 
  19.               $this->t = $t; 
  20.         } 
  21.         public function echo(){ 
  22.                 $this->t->echo(); 
  23.         } 
  24.  

將T抽象出為接口,這樣,EchoT類中的echo方法變成一個(gè)抽象的方法,不到運(yùn)行那一刻,不知道他們的Method方式是怎么實(shí)現(xiàn)的。

工廠

  1. function getT($str) { 
  2.     if(class_exists($str)){ 
  3.         return new $str(); 
  4.         } 
  5.  

T要使用哪個(gè)是不明確的,因此,我們可以將其工廠化。【看上去很簡(jiǎn)單,在DI實(shí)際上有體現(xiàn)】

DI(重點(diǎn)來了)

首先,我們看一下PHP的psr規(guī)范。

http://www.php-fig.org/psr/psr-11/

官方定義的接口

Psr\Container\ContainerInterface

包含兩個(gè)方法

function get($id);

function has($id);

仔細(xì)看上面的工廠,是不是和get($id)很一致,PHP官方將其定義為容器(Container,我個(gè)人理解,就是一個(gè)復(fù)雜的工廠)

dependency injection container

依賴注入容器

  1. namespace Core; 
  2. use Psr\Container\ContainerInterface; 
  3. class Container implements ContainerInterface 
  4.         protected $instance = [];//對(duì)象存儲(chǔ)的數(shù)組 
  5.         public function __construct($path) { 
  6.                 $this->_autoload($path);  //首先我們要自動(dòng)加載  psr-autoload 
  7.         } 
  8.  
  9.         public function build($className) 
  10.         { 
  11.                 if(is_string($className) and $this->has($className)) { 
  12.                         return $this->get($className); 
  13.                 } 
  14.                 //反射 
  15.                 $reflector = new \ReflectionClass($className); 
  16.                 if (!$reflector->isInstantiable()) { 
  17.                         throw new \Exception("Can't instantiate ".$className); 
  18.                 } 
  19.                 // 檢查類是否可實(shí)例化, 排除抽象類abstract和對(duì)象接口interface 
  20.                 if (!$reflector->isInstantiable()) { 
  21.                         throw new \Exception("Can't instantiate ".$className); 
  22.                 } 
  23.                 /** @var \ReflectionMethod $constructor 獲取類的構(gòu)造函數(shù) */ 
  24.                 $constructor = $reflector->getConstructor(); 
  25.                 // 若無構(gòu)造函數(shù),直接實(shí)例化并返回 
  26.                 if (is_null($constructor)) { 
  27.                         return new $className; 
  28.                 } 
  29.                 // 取構(gòu)造函數(shù)參數(shù),通過 ReflectionParameter 數(shù)組返回參數(shù)列表 
  30.                 $parameters = $constructor->getParameters(); 
  31.                 // 遞歸解析構(gòu)造函數(shù)的參數(shù) 
  32.                 $dependencies = $this->getDependencies($parameters); 
  33.                 // 創(chuàng)建一個(gè)類的新實(shí)例,給出的參數(shù)將傳遞到類的構(gòu)造函數(shù)。 
  34.                 $class =  $reflector->newInstanceArgs($dependencies); 
  35.                 $this->instance[$className] = $class; 
  36.                 return $class; 
  37.         } 
  38.  
  39.         /** 
  40.          * @param array $parameters 
  41.          * @return array 
  42.          */ 
  43.         public function getDependencies(array $parameters) 
  44.         { 
  45.                 $dependencies = []; 
  46.                 /** @var \ReflectionParameter $parameter */ 
  47.                 foreach ($parameters as $parameter) { 
  48.                         /** @var \ReflectionClass $dependency */ 
  49.                         $dependency = $parameter->getClass(); 
  50.                         if (is_null($dependency)) { 
  51.                                 // 是變量,有默認(rèn)值則設(shè)置默認(rèn)值 
  52.                                 $dependencies[] = $this->resolveNonClass($parameter); 
  53.                         } else { 
  54.                                 // 是一個(gè)類,遞歸解析 
  55.                                 $dependencies[] = $this->build($dependency->name); 
  56.                         } 
  57.                 } 
  58.                 return $dependencies; 
  59.         } 
  60.  
  61.         /** 
  62.          * @param \ReflectionParameter $parameter 
  63.          * @return mixed 
  64.          * @throws \Exception 
  65.          */ 
  66.         public function resolveNonClass(\ReflectionParameter $parameter) 
  67.         { 
  68.                 // 有默認(rèn)值則返回默認(rèn)值 
  69.                 if ($parameter->isDefaultValueAvailable()) { 
  70.                         return $parameter->getDefaultValue(); 
  71.                 } 
  72.                 throw new \Exception($parameter->getName().' must be not null'); 
  73.         } 
  74.         /** 
  75.          * 參照psr-autoload規(guī)范 
  76.          * @param $path 
  77.          */ 
  78.         public function _autoload($path) { 
  79.                 spl_autoload_register(function(string $class) use ($path) { 
  80.                         $file = DIRECTORY_SEPARATOR.str_replace('\\',DIRECTORY_SEPARATOR, $class).'.php'; 
  81.                         if(is_file($path.$file)) { 
  82.                                 include($path.$file); 
  83.                                 return true
  84.                         } 
  85.                         return false
  86.                 }); 
  87.         } 
  88.  
  89.         public function get($id) 
  90.         { 
  91.                 if($this->has($id)) { 
  92.                         return $this->instance[$id]; 
  93.                 } 
  94.                 if(class_exists($id)){ 
  95.                         return $this->build($id); 
  96.                 } 
  97.                 throw new ClassNotFoundException('class not found');  //實(shí)現(xiàn)的PSR規(guī)范的異常 
  98.         } 
  99.  
  100.         public function has($id) 
  101.         { 
  102.                 return isset($this->instance[$id]) ? true : false
  103.         } 
  104.  

使用示例

  1. $container = new Container('../');//假設(shè)這是路徑 
  2. $echoT = $container->get(\Test\EchoT::class);     //假設(shè)echoT類的命名空間是\Test 
  3. $echoT->echo();  

這個(gè)時(shí)候,會(huì)出現(xiàn)一個(gè)問題:

  1. // 檢查類是否可實(shí)例化, 排除抽象類abstract和對(duì)象接口interface 
  2.                 if (!$reflector->isInstantiable()) { 
  3.                         throw new \Exception("Can't instantiate ".$className);  

因?yàn)榻涌赥是無法實(shí)例化的,所以,一般在程序內(nèi),我們都加上別名(參照l(shuí)aravel框架)

  1. $container->alisa(\Test\T::class,\Test\T\A::class); //指定接口T使用類A(控制反轉(zhuǎn)) 

針對(duì)接口

下面是alias方法 

  1. public function alias(string $key, $class, bool $singleton = true)  
  2.        { 
  3.                if($singleton) { 
  4.                        $this->singleton[] = $class; 
  5.                } 
  6.                $this->aliases[$key] = $class; 
  7.                return $this; 
  8.        } 
  9.    //同時(shí),我們需要在build的時(shí)候進(jìn)行判斷是否為別名 
  10. public function build($className) 
  11.        { 
  12.                if(is_string($className) and $this->has($className)) { 
  13.                        return $this->get($className); 
  14.                } 
  15.                if(isset($this->aliases[$className])) { 
  16.                        if(is_object($this->aliases[$className])) { 
  17.                               return $this->aliases[$className]; 
  18.                        } 
  19.                        $className = $this->aliases[$className]; 
  20.                }  

就此,一個(gè)簡(jiǎn)單的PHP容器就實(shí)現(xiàn)了。 

責(zé)任編輯:龐桂玉 來源: o0無憂亦無怖的博客
相關(guān)推薦

2018-03-12 10:02:30

PHP依賴注入

2015-09-02 11:22:36

JavaScript實(shí)現(xiàn)思路

2011-05-31 10:00:21

Android Spring 依賴注入

2010-05-19 14:54:33

2023-07-11 09:14:12

Beanquarkus

2011-03-01 13:45:41

Spring3Annotation

2022-12-29 08:54:53

依賴注入JavaScript

2013-12-09 09:57:37

2016-10-20 19:36:01

androiddagger2依賴注入

2025-02-17 00:00:55

NET開發(fā)依賴注入

2020-08-06 00:14:16

Spring IoC依賴注入開發(fā)

2019-09-18 18:12:57

前端javascriptvue.js

2022-04-30 08:50:11

控制反轉(zhuǎn)Spring依賴注入

2013-12-13 10:45:26

2010-10-14 09:05:36

ASP.NET MVC

2012-12-19 10:36:06

2024-12-30 12:00:00

.NET Core依賴注入屬性注入

2024-04-01 00:02:56

Go語(yǔ)言代碼

2025-01-13 00:13:59

VSCode架構(gòu)依賴注入

2024-05-27 00:13:27

Go語(yǔ)言框架
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)