欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP設(shè)計模式之工廠模式與單例模式

 更新時間:2016年09月28日 15:33:05   作者:ligbee  
這篇文章主要介紹了PHP設(shè)計模式之工廠模式與單例模式,簡單介紹的工廠模式與單例模式的功能,并結(jié)合實例形式分析了工廠模式及單例模式的實現(xiàn)與應(yīng)用,需要的朋友可以參考下

本文實例講述了PHP設(shè)計模式之工廠模式與單例模式實現(xiàn)方法。分享給大家供大家參考,具體如下:

設(shè)計模式簡單說應(yīng)對某類問題而設(shè)計的解決方式

工廠模式:應(yīng)對需求創(chuàng)建相應(yīng)的對象

class factory{
  function __construct($name){
    if(file_exists('./'.$name.'.class.php')){
      return new $name;
    }else{
      die('not exist');
    }
  }
}

單例模式:只創(chuàng)建一個對象的實例,不允許再創(chuàng)建實例,節(jié)約資源(例如數(shù)據(jù)庫的連接)

class instance{
  public $val = 10;
  private static $instance ;
  private function __construct(){}
  private function __clone(){}
  //設(shè)置為靜態(tà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出一個新的的對象
//$obj_two = clone $obj_one;
$obj_two = instance::getInstance();
echo $obj_two->val;
echo '<p>';
var_dump($obj_one,$obj_two);

運行結(jié)果如下:

20
object(instance)[1]
 public 'val' => int 20
object(instance)[1]
 public 'val' => int 20

應(yīng)用:數(shù)據(jù)庫連接類(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è)計入門教程》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • php mysqli查詢語句返回值類型實例分析

    php mysqli查詢語句返回值類型實例分析

    這篇文章主要介紹了php mysqli查詢語句返回值類型,結(jié)合實例形式分析了php+mysqli常用的查詢、插入語句的使用與返回值類型,需要的朋友可以參考下
    2016-06-06
  • php學習筆記之字符串常見操作總結(jié)

    php學習筆記之字符串常見操作總結(jié)

    這篇文章主要介紹了php學習筆記之字符串常見操作,結(jié)合實例形式總結(jié)分析了php字符串的定義、單引號與雙引號的用法以及常見字符串操作函數(shù)使用技巧,需要的朋友可以參考下
    2019-07-07
  • 深入解析PHP中逗號與點號的區(qū)別

    深入解析PHP中逗號與點號的區(qū)別

    大部分同學都知道逗號要比點號快,但就是不知道為什么,更不知道逗號與點號這兩者之間到底有什么區(qū)別。下面小編就來詳細的為大家介紹一下,需要的朋友可以過來參考下
    2013-08-08
  • php文件上傳類完整實例

    php文件上傳類完整實例

    這篇文章主要介紹了php文件上傳類,結(jié)合完整實例形式分析了php上傳文件的類型判斷、大小計算機限制等技巧,需要的朋友可以參考下
    2016-05-05
  • memcached使用中避坑實例匯總

    memcached使用中避坑實例匯總

    這篇文章主要介紹了memcached使用中避坑實例匯總的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • PHP簡單判斷iPhone、iPad、Android及PC設(shè)備的方法

    PHP簡單判斷iPhone、iPad、Android及PC設(shè)備的方法

    這篇文章主要介紹了PHP簡單判斷iPhone、iPad、Android及PC設(shè)備的方法,可有效的判斷出移動設(shè)備與PC端類型,需要的朋友可以參考下
    2016-10-10
  • PHP的autoload機制的實現(xiàn)解析

    PHP的autoload機制的實現(xiàn)解析

    在使用PHP的OO模式開發(fā)系統(tǒng)時,通常大家習慣上將每個類的實現(xiàn)都存放在一個單獨的文件里,這樣會很容易實現(xiàn)對類進行復用,同時將來維護時也很便利
    2012-09-09
  • Fatal error: ''break'' not in the ''loop'' or ''switch'' context in Function.php

    Fatal error: ''break'' not in the ''loop'' or ''switch'' con

    PHPexcel報出錯誤Fatal error: 'break' not in the 'loop' or 'switch' context in Function.php on line 463.,需要的朋友可以參考下
    2021-06-06
  • PHP連接access數(shù)據(jù)庫

    PHP連接access數(shù)據(jù)庫

    在PHP中連接access數(shù)據(jù)庫的話我們必須ADO來連接,這跟ASP中連接數(shù)據(jù)庫非常的類似.下邊給出了一段DEMO供大家參考.
    2008-03-03
  • 分割GBK中文遭遇亂碼的解決方法

    分割GBK中文遭遇亂碼的解決方法

    以下是對分割GBK中文遭遇亂碼的解決方法進行了詳細的分析介紹,需要的朋友可以過來參考下
    2013-08-08

最新評論