php设计模式之单例模式

单例模式的要点有三个:

1. 一是某个类只能有一个实例;

2. 二是它必须自行创建这个实例;

3. 三是它必须自行向整个系统提供这个实例。

为什么要使用PHP单例模式

1. php的应用主要在于数据库应用, 一个应用中会存在大量的数据库操作, 在使用面向对象的方式开发时, 如果使用单例模式, 则可以避免大量的new 操作消耗的资源,还可以减少数据库连接这样就不容易出现 too many connections情况。

2. 如果系统中需要有一个类来全局控制某些配置信息, 那么使用单例模式可以很方便的实现. 这个可以参看zend Framework的FrontController部分。

3. 在一次页面请求中, 便于进行调试, 因为所有的代码(例如数据库操作类db)都集中在一个类中, 我们可以在类中设置钩子, 输出日志,从而避免到处var_dump, echo。

下面来个单例的例子

/**
 * 设计模式之单例模式
 * $_instance必须声明为静态的私有变量
 * 构造函数必须声明为私有,防止外部程序new类从而失去单例模式的意义
 * getInstance()方法必须设置为公有的,必须调用此方法以返回实例的一个引用
 * ::操作符只能访问静态变量和静态函数
 * new对象都会消耗内存
 * 使用场景:最常用的地方是数据库连接。
 * 使用单例模式生成一个对象后,该对象可以被其它众多对象所使用。
 */

class single{
private static $instance;
private function __construct(){
echo "WTF I'll be new!!";
}
public static function getInstance(){
    var_dump(self::$instance);
if(!isset(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
private function __clone(){
  trigger_error('Clone is not allow' ,E_USER_ERROR);
}
public function run(){
echo 'what the fuck man!!!!!';
}
}
$single = single::getInstance();

$single->run();

下面来个数据库的单例,下面的代码为转载网上的一个实例

 <?php
 2 /*
 3 * mysql 单例
 4 */
 5 class mysql{
 6     private $host    ='localhost'; //数据库主机
 7     private $user     = 'root'; //数据库用户名
 8     private $pwd     = ''; //数据库用户名密码
 9     private $database = 'test'; //数据库名
10     private $charset = 'utf8'; //数据库编码,GBK,UTF8,gb2312
11     private $link;             //数据库连接标识;
12     private $rows;             //查询获取的多行数组
13     static $_instance; //存储对象
14     /**
15      * 构造函数
16      * 私有
17      */
18     private function __construct($pconnect = false) {
19         if (!$pconnect) {
20             $this->link = @ mysql_connect($this->host, $this->user, $this->pwd) or $this->err();
21         } else {
22             $this->link = @ mysql_pconnect($this->host, $this->user, $this->pwd) or $this->err();
23         }
24         mysql_select_db($this->database) or $this->err();
25         $this->query("SET NAMES '{$this->charset}'", $this->link);
26         return $this->link;
27     }
28     /**
29      * 防止被克隆
30      *
31      */
32     private function __clone(){}
33     public static function getInstance($pconnect = false){
34         if(FALSE == (self::$_instance instanceof self)){
35             self::$_instance = new self($pconnect);
36         }
37         return self::$_instance;
38     }
39     /**
40      * 查询
41      */
42     public function query($sql, $link = '') {
43         $this->result = mysql_query($sql, $this->link) or $this->err($sql);
44         return $this->result;
45     }
46     /**
47      * 单行记录
48      */
49     public function getRow($sql, $type = MYSQL_ASSOC) {
50         $result = $this->query($sql);
51         return @ mysql_fetch_array($result, $type);
52     }
53     /**
54      * 多行记录
55      */
56     public function getRows($sql, $type = MYSQL_ASSOC) {
57         $result = $this->query($sql);
58         while ($row = @ mysql_fetch_array($result, $type)) {
59             $this->rows[] = $row;
60         }
61         return $this->rows;
62     }
63     /**
64      * 错误信息输出
65      */
66     protected function err($sql = null) {
67         //这里输出错误信息
68         echo 'error';
69         exit();
70     }
71 }
72 //用例
73 $db = mysql::getInstance();
74 $db2 = mysql::getInstance();
75 $data = $db->getRows('select * from test');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值