PHP經(jīng)典設(shè)計(jì)模式之依賴注入定義與用法詳解
本文實(shí)例講述了PHP經(jīng)典設(shè)計(jì)模式之依賴注入定義與用法。分享給大家供大家參考,具體如下:
依賴注入的實(shí)質(zhì)就是把一個(gè)類(lèi)不可能更換的部分和可更換的部分分離開(kāi)來(lái),通過(guò)注入的方式來(lái)使用,從而達(dá)到解耦的目的。
一個(gè)數(shù)據(jù)庫(kù)連接類(lèi):
class Mysql{
private $host;
private $prot;
private $username;
private $password;
private $db_name;
// 構(gòu)造方法
public function __construct(){
$this->host = '127.0.0.1';
$this->port = 22;
$this->username = 'root';
$this->password = '';
$this->db_name = 'my_db';
}
// 連接
public function connect(){
return mysqli_connect($this->host,$this->username,$this->password,$this->db_name,$this->port);
}
}
使用這個(gè)類(lèi):
$db = new Mysql(); $db->connect();
通常數(shù)據(jù)庫(kù)連接類(lèi)應(yīng)該設(shè)計(jì)為單列,這里先不要搞復(fù)雜了。
依賴注入
顯然,數(shù)據(jù)庫(kù)的配置是可以更換的部分,因此我們需要先把它拎出來(lái):
class MysqlConfiguration{
private $host;
private $prot;
private $username;
private $password;
private $db_name;
public function __construct($host,$port,$username,$password,$db_name){
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->db_name = $db_name;
}
public function getHost(){
return $this->host;
}
public function getPort(){
return $this->port();
}
public function getUsername(){
return $this->username;
}
public function getPassword(){
return $this->password;
}
public function getDbName(){
return $this->db_name;
}
}
然后不可替換的部分這樣:
class Mysql{
private $configuration;
public function __construct($config){
$this->configuration = $config;
}
// 連接
public function connect(){
return mysqli_connect($this->configuration->getHost(),$this->configuration->getUsername(),$this->configuration->getPassword(),$this->configuration->getDbName(),$this->configuration->getPort());
}
}
這樣就完成了配置文件和連接邏輯的分離。
使用
$config = new MysqlConfiguration('127.0.0.1','root','password','my_db',22);
// $config是注入Mysql的,這就是所謂的依賴注入
$db = new Mysql($config);
$db->connect();
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP處理JSON字符串key缺少雙引號(hào)的解決方法
這篇文章主要介紹了PHP處理JSON字符串key缺少雙引號(hào)的解決方法,是非常常見(jiàn)的一類(lèi)錯(cuò)誤處理情況,需要的朋友可以參考下2014-09-09
解決php使用異步調(diào)用獲取數(shù)據(jù)時(shí)出現(xiàn)(錯(cuò)誤c00ce56e導(dǎo)致此項(xiàng)操作無(wú)法完成)
本篇文章是對(duì)php中使用異步調(diào)用獲取數(shù)據(jù)時(shí)出現(xiàn)(由于出現(xiàn)錯(cuò)誤c00ce56e而導(dǎo)致此項(xiàng)操作無(wú)法完成)的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
PHP實(shí)現(xiàn)限制IP訪問(wèn)及提交次數(shù)的方法詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)限制IP訪問(wèn)及提交次數(shù)的方法,涉及php針對(duì)客戶端來(lái)訪IP的獲取、判斷以及結(jié)合session記錄IP訪問(wèn)次數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
round robin權(quán)重輪循算法php實(shí)現(xiàn)代碼
這篇文章主要介紹了round robin權(quán)重輪循算法php實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-05-05
php恢復(fù)數(shù)組的key為數(shù)字序列的方法
這篇文章主要介紹了php恢復(fù)數(shù)組的key為數(shù)字序列的方法,涉及php操作數(shù)組鍵名的技巧,需要的朋友可以參考下2015-04-04
php 過(guò)濾英文標(biāo)點(diǎn)符號(hào)及過(guò)濾中文標(biāo)點(diǎn)符號(hào)代碼
這篇文章主要介紹了php過(guò)濾英文標(biāo)點(diǎn)符號(hào)及過(guò)濾中文標(biāo)點(diǎn)符號(hào)的方法,需要的朋友可以參考下2014-06-06
php+mysqli事務(wù)控制實(shí)現(xiàn)銀行轉(zhuǎn)賬實(shí)例
這篇文章主要介紹了php+mysqli事務(wù)控制實(shí)現(xiàn)銀行轉(zhuǎn)賬,實(shí)例分析了事物控制的原理與事物回滾的使用技巧,需要的朋友可以參考下2015-01-01

