PHP設(shè)計(jì)模式之工廠模式與單例模式
本文實(shí)例講述了PHP設(shè)計(jì)模式之工廠模式與單例模式實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
設(shè)計(jì)模式簡(jiǎn)單說(shuō)應(yīng)對(duì)某類(lèi)問(wèn)題而設(shè)計(jì)的解決方式
工廠模式:應(yīng)對(duì)需求創(chuàng)建相應(yīng)的對(duì)象
class factory{ function __construct($name){ if(file_exists('./'.$name.'.class.php')){ return new $name; }else{ die('not exist'); } } }
單例模式:只創(chuàng)建一個(gè)對(duì)象的實(shí)例,不允許再創(chuàng)建實(shí)例,節(jié)約資源(例如數(shù)據(jù)庫(kù)的連接)
class instance{ public $val = 10; private static $instance ; private function __construct(){} private function __clone(){} //設(shè)置為靜態(tài)方法才可被類(lèi)調(diào)用 public static function getInstance(){ /*if(!isset(self::$instance)){ self::$instance = new self; }*/ if(!isset(instance::$instance)){ instance::$instance = new self; } return instance::$instance; } } $obj_one = instance::getInstance(); $obj_one->val = 20; //clone可以調(diào)用__clone()克隆即new出一個(gè)新的的對(duì)象 //$obj_two = clone $obj_one; $obj_two = instance::getInstance(); echo $obj_two->val; echo '<p>'; var_dump($obj_one,$obj_two);
運(yùn)行結(jié)果如下:
20 object(instance)[1] public 'val' => int 20 object(instance)[1] public 'val' => int 20
應(yīng)用:數(shù)據(jù)庫(kù)連接類(lèi)(database access oject)
class mysqldb{ private $arr = array( 'port' => 3306, 'host' => 'localhost', 'username' => 'root', 'passward' => 'root', 'dbname' => 'instance', 'charset' => 'utf8' ); private $link; static $instance; private function __clone(){} private function __construct(){ $this->link = mysql_connect($this->arr['host'],$this->arr['username'],$this->arr['passward']) or die(mysql_error()); mysql_select_db($this->arr['dbname']) or die('db error'); mysql_set_charset($this->arr['charset']); } static public function getInsance(){ if(!isset(mysqldb::$instance)){ mysqldb::$instance = new self; } return mysqldb::$instance; } public function query($sql){ if($res = mysql_query($sql)){ return $res; }return false; } //fetch one public function get_one($sql){ $res = $this->query($sql); if($result = mysql_fetch_row($res)){ return $result[0]; } } //fetch row public function get_row($sql){ $res = $this->query($sql); if($result = mysql_fetch_assoc($res)){ return $result; } return false; } //fetch all public function get_all($sql){ $res = $this->query($sql); $arr = array(); while($result = mysql_fetch_assoc($res)){ $arr[] = $result; } return $arr; } } $mysql = mysqldb::getInsance();
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php mysqli查詢語(yǔ)句返回值類(lèi)型實(shí)例分析
這篇文章主要介紹了php mysqli查詢語(yǔ)句返回值類(lèi)型,結(jié)合實(shí)例形式分析了php+mysqli常用的查詢、插入語(yǔ)句的使用與返回值類(lèi)型,需要的朋友可以參考下2016-06-06php學(xué)習(xí)筆記之字符串常見(jiàn)操作總結(jié)
這篇文章主要介紹了php學(xué)習(xí)筆記之字符串常見(jiàn)操作,結(jié)合實(shí)例形式總結(jié)分析了php字符串的定義、單引號(hào)與雙引號(hào)的用法以及常見(jiàn)字符串操作函數(shù)使用技巧,需要的朋友可以參考下2019-07-07深入解析PHP中逗號(hào)與點(diǎn)號(hào)的區(qū)別
大部分同學(xué)都知道逗號(hào)要比點(diǎn)號(hào)快,但就是不知道為什么,更不知道逗號(hào)與點(diǎn)號(hào)這兩者之間到底有什么區(qū)別。下面小編就來(lái)詳細(xì)的為大家介紹一下,需要的朋友可以過(guò)來(lái)參考下2013-08-08PHP簡(jiǎn)單判斷iPhone、iPad、Android及PC設(shè)備的方法
這篇文章主要介紹了PHP簡(jiǎn)單判斷iPhone、iPad、Android及PC設(shè)備的方法,可有效的判斷出移動(dòng)設(shè)備與PC端類(lèi)型,需要的朋友可以參考下2016-10-10PHP的autoload機(jī)制的實(shí)現(xiàn)解析
在使用PHP的OO模式開(kāi)發(fā)系統(tǒng)時(shí),通常大家習(xí)慣上將每個(gè)類(lèi)的實(shí)現(xiàn)都存放在一個(gè)單獨(dú)的文件里,這樣會(huì)很容易實(shí)現(xiàn)對(duì)類(lèi)進(jìn)行復(fù)用,同時(shí)將來(lái)維護(hù)時(shí)也很便利2012-09-09Fatal error: ''break'' not in the ''loop'' or ''switch'' con
PHPexcel報(bào)出錯(cuò)誤Fatal error: 'break' not in the 'loop' or 'switch' context in Function.php on line 463.,需要的朋友可以參考下2021-06-06